ArrayList<HashMap<String, String>> mylist = new ArrayList<>();
HashMap<String, String> map1 = new HashMap<>();
map1.put("Country", "India");
map1.put("State", "Tamilnadu");
map1.put("Distict", "Ariyalur");
HashMap<String, String> map2 = new HashMap<>();
map2.put("Country", "India");
map2.put("State", "Tamilnadu");
map2.put("Distict", "Salem");
HashMap<String, String> map3 = new HashMap<>();
map3.put("Country", "India");
map3.put("State", "Tamilnadu");
map3.put("Distict", "Trichy");
HashMap<String, String> map4 = new HashMap<>();
map4.put("Country", "India");
map4.put("State", "Kerala");
map4.put("Distict", "State1");
HashMap<String, String> map5 = new HashMap<>();
map5.put("Country", "Pak");
map5.put("State", "Kerala");
map5.put("Distict", "State1");
mylist.add(map1);
mylist.add(map2);
mylist.add(map3);
mylist.add(map4);
mylist.add(map5);
System.out.println(mylist);
ArrayList<HashMap<String, String>> counstatelist = new ArrayList<>();
for (int i = 0; i < mylist.size(); i++) {
HashMap<String, String> tempMap = new HashMap<>();
if (counstatelist.size() > 0) {
for (int j = 0; j < counstatelist.size(); j++) {
if ((!counstatelist.get(j).get("Country")
.equals(mylist.get(i).get("Country")))
|| (!counstatelist.get(j).get("State")
.equals(mylist.get(i).get("State")))) {
tempMap.put("Country", mylist.get(i).get("Country"));
tempMap.put("State", mylist.get(i).get("State"));
counstatelist.add(tempMap);
}
}
} else {
tempMap.put("Country", mylist.get(i).get("Country"));
tempMap.put("State", mylist.get(i).get("State"));
counstatelist.add(tempMap);
}
}
System.out.println(counstatelist);
In this code I want to get group of Country and State list.
So my expected output is
[{State=Tamilnadu, Country=India}, {State=Kerala, Country=India}, {State=Kerala, Country=Pak}]
But this code gives me following output
[{State=Tamilnadu, Country=India}, {State=Kerala, Country=India}, {State=Kerala, Country=Pak}, {State=Kerala, Country=Pak}]
Update 1 I have changed the logic as following.
for (int i = 0; i < mylist.size(); i++) {
HashMap<String, String> tempMap = new HashMap<>();
if (counstatelist.size() > 0) {
for (int j = 0; j < counstatelist.size(); j++) {
if ((!counstatelist.get(j).get("State")
.equals(mylist.get(i).get("State")))) {
tempMap.put("Country", mylist.get(i).get("Country"));
tempMap.put("State", mylist.get(i).get("State"));
counstatelist.add(tempMap);
}
}
} else {
tempMap.put("Country", mylist.get(i).get("Country"));
tempMap.put("State", mylist.get(i).get("State"));
counstatelist.add(tempMap);
}
}
Now the expected output is came. But I don't know how this works. I don't know what I am doing wrong,
Update 2
I have updated mylist with following data.
HashMap<String, String> map1 = new HashMap<>();
map1.put("Country", "India");
map1.put("State", "Tamilnadu");
map1.put("Distict", "Ariyalur");
HashMap<String, String> map2 = new HashMap<>();
map2.put("Country", "India");
map2.put("State", "Tamilnadu");
map2.put("Distict", "Salem");
HashMap<String, String> map3 = new HashMap<>();
map3.put("Country", "India");
map3.put("State", "Tamilnadu");
map3.put("Distict", "Trichy");
HashMap<String, String> map4 = new HashMap<>();
map4.put("Country", "India");
map4.put("State", "Kerala");
map4.put("Distict", "State1");
HashMap<String, String> map5 = new HashMap<>();
map5.put("Country", "Pak");
map5.put("State", "Kerala");
map5.put("Distict", "State1");
HashMap<String, String> map6 = new HashMap<>();
map6.put("Country", "India");
map6.put("State", "Tamilnadu");
map6.put("Distict", "Trichy");
mylist.add(map1);
mylist.add(map2);
mylist.add(map3);
mylist.add(map4);
mylist.add(map5);
mylist.add(map6);
Now the output is
[{State=Tamilnadu, Country=India}, {State=Kerala, Country=India}, {State=Kerala, Country=Pak}, {State=Tamilnadu, Country=India}, {State=Tamilnadu, Country=India}]
But the output should be
[{State=Tamilnadu, Country=India}, {State=Kerala, Country=India}, {State=Kerala, Country=Pak}]
Thanks,
Guna
extendsand ArrayList, containing things that extendHashMaps. An arraylist of hashmaps of strings to strings is an almost guaranteed "this needs its own datastructure" signal.