简体   繁体   中英

Compare HashMap values in an ArrayList

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);
        }   
    }
}
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...

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!

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());
        }
    }
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("raj@gmail.com");

        CustomerContactVO v1 = new CustomerContactVO();
        v1.setContactAcctID("60011152");
        v1.setEmailID("raj1@gmail.com");

        CustomerContactVO v2 = new CustomerContactVO();
        v2.setContactAcctID("60011153");
        v2.setEmailID("raj2@gmail.com");

        CustomerContactVO v3 = new CustomerContactVO();
        v3.setContactAcctID("60011154");
        v3.setEmailID("raj3@gmail.com");

        CustomerContactVO v4 = new CustomerContactVO();
        v4.setContactAcctID("60011155");
        v4.setEmailID("raj4@gmail.com");

        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());
        }
    }

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM