2011-10-27 94 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