我有一个ArrayList
,其中有一些HashMap<String, String>
。所以,我想在地图中比较相同的值。当我找到相同的值时,我想保留一张它们的地图。例如,考虑第二个地图和第五个地图(在arraylist中)具有相同的值。我想保留第二张地图并从arraylist中删除第五张。
我尝试用迭代器,但我不能这样做。这似乎很复杂。你能举个例子吗?
这是我的最后一次尝试:
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 :(得分: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的重叠值,但地图2被删除,因为地图1的某些其他重重值。结果:仅保留地图1并删除地图2和3但是地图3没有地图1的欺骗。这比我想象的要复杂一点。最好喝咖啡......
答案 1 :(得分:1)
只是大声思考,但我的方法是这样的:
创建一个Set,您可以在其中存储已在地图中找到的值。
每次在列表的新位置获取Map时,检查Set中是否存在Map的元素,如果是,则从ArrayList中删除Map(它是重复的),如果没有,将Map的值添加到Set and Carry on。
确保使用Iterator的remove方法从ArrayList中删除Map!
答案 2 :(得分:0)
创建Set<HashMap<String,String>>
并将list
的每个成员添加到其中。问题解决了!
如果你绝对需要一个ArrayList
而不是Set
,你可以从ArrayList
创建一个新的Set
,但无论如何都是这样的:让Java做为你工作。与标准库相比,您不太可能在集合处理方面做得更好。
答案 3 :(得分: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());
}
}
答案 4 :(得分: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("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());
}
}
}