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);
}
}
}
ArrayList(capita A and L) andHashMap(capital H and M)