【问题标题】:Compare HashMap values in an ArrayList比较 ArrayList 中的 HashMap 值
【发布时间】:2011-12-16 08:56:17
【问题描述】:

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

这是我最后一次尝试:

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

【问题讨论】:

  • 我将其重新标记为 Java,因为您说的是 ArrayList(大写 A 和 L)和 HashMap(大写 H 和 M)
  • 您如何看待地图具有相同的价值?有一个相同的元素,具有相同的键,还是具有完全相同的一组元素?这真的取决于!请澄清一下你的答案。
  • 请发布一个显示您的数据结构的代码示例。
  • @pcalcao 我的意思是元素。没有键,没有设置。
  • 如果地图 1 的值为 {1, 2},地图 2 的值为 {1, 3},地图 3 的值为 {3, 4},会发生什么?您必须删除地图 2,因为它与地图 1 共享一个值,然后地图 3 与地图 2 共享一个值,但与地图 1 不共享。地图 3 会发生什么?这真的不简单。如果我们找出您的问题的背景可能会更好。我认为你最好使用不同的数据结构,或者在使用数据时留下冲突并解决它们......

标签: java algorithm arraylist compare hashmap


【解决方案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());
}

希望有人对此进行校对,因为我没有在 IDE 中输入它,而且我还没有喝咖啡。

编辑:好的,以前的版本是错误的。请改为检查。

编辑 2:刚刚意识到这也行不通。例如,它可能会删除地图 3,因为它与地图 2 具有重复值,但由于地图 1 的某些其他重复值而删除了地图 2。结果:仅保留地图 1,地图 2 和 3 被删除,但地图 3没有地图 1 的欺骗。这比我想象的要复杂一些。最好喝杯咖啡……

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      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());
              }
          }
      
      }
      

      【讨论】:

      • 虽然这段代码可以回答这个问题,但最好也提供一些解释来解释你的推理及其作用。
      【解决方案4】:

      只是大声思考,但我的方法是这样的:

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

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

      确保使用迭代器的 remove 方法从 ArrayList 中删除 Map!

      【讨论】:

      • 我一开始的想法,但这仍然留下了我所描述的“传递性”碰撞问题。
      • 不确定我是否在关注。如果您删除了整个地图,就会发生这种情况。如果您仅删除该值,则不会。当然,您可以添加一个行为来检查地图是否为空(在这种情况下,它的所有元素都会出现在以前的地图中)。
      • 但从提问者的措辞来看,他似乎确实想删除整个地图,而不仅仅是一个地图条目。
      • 那部分不清楚:/如果是这样,你是绝对正确的。
      • 如果每个映射只需要一个条目,您最好自己制作一个小型数据结构,将键与值链接起来,并提供一些为您的用例提供便利的方法。
      【解决方案5】:

      创建一个Set&lt;HashMap&lt;String,String&gt;&gt; 并将list 的每个成员添加到其中。问题解决了!

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

      【讨论】:

      • 我认为他不只是想检查整个地图的相等性,而是对地图之间的单个值冲突采取行动。
      猜你喜欢
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2015-01-19
      • 2017-08-30
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多