1

I have an ArrayList and there are some HashMap<String, String> in this. So, I want to compare for same values in the maps. When I find same values then I want to keep one map of them. For example, consider that second map and fifth map (in the arraylist) have the same value. I want to keep the second map and remove the fifth from the arraylist. i try to do with an iterator, but i can't do it. It seems complicated. Can you give me an example?

This is my last try:

private HashMap<String, String> mapValues = new HashMap<String, String>();
private HashMap<String, String> mapValues2 = new HashMap<String,String>(); 
private HashMap<Integer, String> mval = new HashMap<Integer, String>();

//i take the ArrayList with the maps for comparison private
ArrayList<HashMap<String, String>> check(ArrayList<HashMap<String, String>> list) {           

 //a new ArrayList. It will have the maps(HashMap<key, value>) with no same values.
 ArrayList<HashMap<String, String>> listFinal = new ArrayList<HashMap<String, String();

    for (int i = 0; i < list.size(); i++) {
        mapValues = list.get(i);
        mval.put(i, mapValues.get("value"));
    }

    for (int i = 0; i < mval.size(); i++) {
        HashMap<String, String> newMapValues = new HashMap<String, String>();
        mapValues2 = list.get(i);
        String iVal = mapValues2.get("value");
        newMapValues = list.get(i);
        int flag = -1;
        int remove = -1;

        for (int j = i+1; j < mval.size()-1; j++) {
            String jVal = mval.get(j);
            if (val.compareTo(jVal) == 0) {
                flag = i;
                remove = j;
            }
        }
        if (flag == -1) {
            listFinal.add(newMapValues );
        } else if (flag != -1) {
            listFinal.remove(remove);
        }   
    }
}
8
  • I retagged it as Java, because you speak of ArrayList (capita A and L) and HashMap (capital H and M) Commented Oct 27, 2011 at 9:28
  • What do you consider for the maps to have the same value? Having one of the elements that's equal, having the same keys, or having exactly the same set of elements? It really depends! Please clarify your answer a bit. Commented Oct 27, 2011 at 9:30
  • Please post a code example showing your data structures. Commented Oct 27, 2011 at 9:30
  • @pcalcao I mean the elements. No keys, no set. Commented Oct 27, 2011 at 9:46
  • What happens if map 1 has values {1, 2}, map 2 has values {1, 3} and map 3 has values {3, 4}? You've have to remove map 2 because it shares a value with map 1, and then map 3 shares a value with map 2 but not with map 1. What happens with map 3? This isn't really simple. Maybe it's better if we find out what the context of your problem is. I think you're better off with a different data structure, or leave the collisions in and resolve them when using the data... Commented Oct 27, 2011 at 9:53

5 Answers 5

1

Just thinking out loud but my approach would be something like:

Create a Set, where you store the values that you already found in the map.

Each time you get a Map in a new position of the list, check if the element of the Map exists in the Set, if it does, remove the Map from the ArrayList (it's duplicated), if it doesn't, add the value of the Map to the Set and Carry on.

Make sure you remove the Map from the ArrayList using the Iterator's remove method!

Sign up to request clarification or add additional context in comments.

8 Comments

What I thought at first, but that still leaves the "transitive" collision problem I've described.
Not sure I'm following. That would happen if you removed the whole map. Not if you remove just the value. Of course you can add a behavior to check if the Map is empty (in which case all of it's elements would have occurred in previous maps).
But from the asker's wording, it does seem as if he wants to remove the entire map, not just a map entry.
That part isn't clear :/ if that's the case, you're absolutely right.
If you only need one entry per map, you're probably better of just making a small datastructure yourself that links a key with a value and have some methods that provide convenience for your use-case.
|
1
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>(); 
//... filling up list and maps...
Set<String> valueSet = new HashSet<String>();
for(Iterator<Map<String, String>> mapIt = mapList.iterator(); mapIt.hasNext();) {
    final Map<String, String> map = mapIt.next();
    boolean hasDuplicate = false;
    for(final String mapValue : map.values()) {
        if(valueSet.contains(mapValue))
            hasDuplicate = true;
    }
    if(hasDuplicate)
        mapIt.remove();
    valueSet.addAll(map.values());
}

Hope someone proofreads this, cause I'm not typing it in an IDE and I haven't had my coffee yet.

EDIT: okay, that previous version was wrong as hell. Check this instead.

EDIT 2: just realized this won't work either. It might remove, say, map 3 because it has a dupe value with map 2, but map 2 is removed because of some other dupe value with map 1. Result: only map 1 is retained and map 2 and 3 are removed but map 3 doesn't have dupes with map 1. This is a bit more complex than I thought. Better get that coffee...

Comments

0

Create a Set<HashMap<String,String>> and add every member of list to it. Problem solved!

If you absolutely need an ArrayList instead of a Set, you can create a new ArrayList from the Set, but either way the lesson is: let Java do the work for you. You are unlikely to do a better job at collection manipulation than the standard library.

1 Comment

I think he doesn't just want to check total map equality but act on even a single value collision between maps.
0
package com.test.examples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.test.vo.CustomerContactVO;
import com.test.vo.CustomerOutPut;
import com.test.vo.CustomerPreferenceVO;

public class TestExampleOne {

    public static Map<String, CustomerContactVO> getVO(){

        Map<String, CustomerContactVO> contactVOMap = new HashMap<String, CustomerContactVO>();
        CustomerContactVO v = new CustomerContactVO();
        v.setContactAcctID("60011151");
        v.setEmailID("[email protected]");

        CustomerContactVO v1 = new CustomerContactVO();
        v1.setContactAcctID("60011152");
        v1.setEmailID("[email protected]");

        CustomerContactVO v2 = new CustomerContactVO();
        v2.setContactAcctID("60011153");
        v2.setEmailID("[email protected]");

        CustomerContactVO v3 = new CustomerContactVO();
        v3.setContactAcctID("60011154");
        v3.setEmailID("[email protected]");

        CustomerContactVO v4 = new CustomerContactVO();
        v4.setContactAcctID("60011155");
        v4.setEmailID("[email protected]");

        contactVOMap.put("60011151", v);
        contactVOMap.put("60011152", v1);
        contactVOMap.put("60011153", v2);
        contactVOMap.put("60011154", v3);
        contactVOMap.put("60011155", v4);

        return contactVOMap;
    }

    public static List<CustomerPreferenceVO> perfVo(){
        CustomerPreferenceVO prefVo = new CustomerPreferenceVO();
        prefVo.setContactAcctID("60011151");
        prefVo.setMktInd("500");
        prefVo.setPrefInd("Y");


        CustomerPreferenceVO prefVo1 = new CustomerPreferenceVO();
        prefVo1.setContactAcctID("60011153");
        prefVo1.setMktInd("302");
        prefVo1.setPrefInd("N");

        CustomerPreferenceVO prefVo2 = new CustomerPreferenceVO();
        prefVo2.setContactAcctID("60011154");
        prefVo2.setMktInd("302");
        prefVo2.setPrefInd("Y");

        List<CustomerPreferenceVO> list = new ArrayList<CustomerPreferenceVO>();
        list.add(prefVo);
        list.add(prefVo1);
        list.add(prefVo2);

        return list;
    }

    public static void main(String[] args) {

        Iterator<Entry<String, CustomerContactVO>> it = getVO().entrySet().iterator();
        List<CustomerOutPut> customerOutPutsList = new ArrayList<CustomerOutPut>();

        while(it.hasNext()){

            Entry<String, CustomerContactVO> ent = it.next();
            String contAcctIDKey = ent.getKey();
            String email = ent.getValue().getEmailID();
            CustomerOutPut customerOutPut = new CustomerOutPut();
            customerOutPut.setContactAcctIDVo(contAcctIDKey);
            customerOutPut.setEmailIDVo(email);

            for (CustomerPreferenceVO customerPreferenceVO : perfVo()) {

                if(customerPreferenceVO.getContactAcctID()!=null && 
                        customerPreferenceVO.getContactAcctID().equals(contAcctIDKey)){

                    customerOutPut.setContactAcctIDRef(customerPreferenceVO.getContactAcctID());
                    customerOutPut.setMktIndRef(customerPreferenceVO.getMktInd());
                    customerOutPut.setPrefIndRef(customerPreferenceVO.getPrefInd());

                }
            }

            customerOutPutsList.add(customerOutPut);
        }

        for (CustomerOutPut customerOutPut : customerOutPutsList) {
            System.out.println(customerOutPut.toString());
        }
    }

}

1 Comment

While this code may answer the question, it is better to also provide some explanations to explain your reasoning and what it does.
0

Compare Map keys with Arraylist values

public static void main(String[] args) {

        Iterator<Entry<String, CustomerContactVO>> it = getVO().entrySet().iterator();

        List<CustomerOutPut> customerOutPutsList = new ArrayList<CustomerOutPut>();

        while(it.hasNext()){
            Entry<String, CustomerContactVO> ent = it.next();
            String contAcctIDKey = ent.getKey();
            String email = ent.getValue().getEmailID();
            CustomerOutPut customerOutPut = new CustomerOutPut();
            customerOutPut.setContactAcctIDVo(contAcctIDKey);
            customerOutPut.setEmailIDVo(email);

            for (CustomerPreferenceVO customerPreferenceVO : perfVo()) {
                if(customerPreferenceVO.getContactAcctID()!=null && customerPreferenceVO.getContactAcctID().equals(contAcctIDKey)){
                    customerOutPut.setContactAcctIDRef(customerPreferenceVO.getContactAcctID());
                    customerOutPut.setMktIndRef(customerPreferenceVO.getMktInd());
                    customerOutPut.setPrefIndRef(customerPreferenceVO.getPrefInd());
                }
            }

            customerOutPutsList.add(customerOutPut);
        }

        for (CustomerOutPut customerOutPut : customerOutPutsList) {
            System.out.println(customerOutPut.toString());
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.