比较 ArrayList 中的 HashMap 值

发布于 2024-12-12 02:29:36 字数 1684 浏览 0 评论 0 原文

我有一个 ArrayList ,其中有一些 HashMap 。所以,我想比较地图中的相同值。当我找到相同的值时,我想保留一张它们的地图。例如,假设第二个映射和第五个映射(在数组列表中)具有相同的值。我想保留第二张地图并从数组列表中删除第五张地图。 我尝试使用迭代器,但我做不到。看起来很复杂。你能举个例子吗?

这是我最后一次尝试:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半步萧音过轻尘 2024-12-19 02:29:36

只是大声思考,但我的方法类似于:

创建一个集合,在其中存储已在地图中找到的值。

每次在列表的新位置获取一个Map时,检查该Map的元素是否存在于Set中,如果存在,则从ArrayList中删除该Map(它是重复的),如果不存在,则添加该值地图的设置并继续。

确保使用 Iterator 的删除方法从 ArrayList 中删除 Map!

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!

旧人哭 2024-12-19 02:29:36
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());
}

希望有人校对一下这个,因为我没有在 IDE 中输入它,而且我还没有喝咖啡。

编辑:好的,以前的版本是错误的。检查这个。

编辑2:刚刚意识到这也行不通。例如,它可能会删除映射 3,因为它具有映射 2 的重复值,但映射 2 会因为映射 1 的其他重复值而被删除。结果:仅保留映射 1,映射 2 和 3 被删除,但映射 3 被删除地图 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...

请帮我爱他 2024-12-19 02:29:36

创建一个 Set> 并将 list 的每个成员添加到其中。问题解决了!

如果您绝对需要 ArrayList 而不是 Set,您可以从 Set 创建一个新的 ArrayList,但是无论哪种方式,教训都是:让 Java 为您完成工作。在集合操作方面,您不太可能比标准库做得更好。

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.

栩栩如生 2024-12-19 02:29:36
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());
        }
    }

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

}
幸福%小乖 2024-12-19 02:29:36

Map 键与 Arraylist 值进行比较

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

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());
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文