2011-10-27 163 views
0

我有一个ArrayList,这里有一些HashMap<String, String>。所以,我想比较地图中的相同值。当我找到相同的值时,我想保留它们的一张地图。例如,考虑第二张地图和第五张地图(在数组列表中)具有相同的值。我想保留第二张地图,并从阵列列表中删除第五张地图。 我试着做一个迭代器,但我做不到。看起来很复杂。你可以给我一个例子吗?将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); 
     } 
    } 
} 
+0

我重新标记它如Java,因为你讲ArrayList'(人均A和L)和'HashMap'(大写的H和M) – xanatos

+0

的'什么你认为地图具有相同的价值吗?有一个相同的元素,拥有相同的密钥,还是具有完全相同的元素集?这真的取决于!请澄清你的答案。 – pcalcao

+0

请发布显示您的数据结构的代码示例。 –

回答

1
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>(); //Assuming string-string pairs for simplicity... 
//... 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的模糊。这比我想象的要复杂一些。更好地获得咖啡...

0

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

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

+0

我认为他不只是想检查总体地图平等,而是要处理地图之间的单个值冲突。 –

1

只是想大声,但我的做法是这样的:

创建一个组,在那里你存储你已经在地图上找到的值。

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

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

+0

我最初的想法是,但仍然留下了我所描述的“过渡性”碰撞问题。 –

+0

不知道我在追随。如果你删除了整个地图,就会发生这种情况。如果您仅删除该值,则不是。当然,你可以添加一个行为来检查Map是否为空(在这种情况下,它的所有元素都将出现在以前的地图中)。 – pcalcao

+0

但是从提问者的措辞来看,他似乎好像要删除整个地图,而不仅仅是地图条目。 –

0

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

} 
+2

虽然这段代码可能回答这个问题,但最好也提供一些解释来解释你的推理和它的作用。 – nha