1
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

5
  • 1
    Consider stating your ordering criteria in a class, and then, solve your problem with Collections.sort(list, myHashMapComparator) Commented Aug 20, 2014 at 5:24
  • on what basis you want to short the list? Commented Aug 20, 2014 at 5:24
  • Use a debugger to see where the logic breaks. Commented Aug 20, 2014 at 5:25
  • Also, you are creating new instances of HashMap just to sort your list. On top of that you lose the district bit of your sorted data. Commented Aug 20, 2014 at 5:26
  • On a completely different note, if you need an arraylist of hashmaps, you may be much better off with a proper class that's named after what you need it for, even if it extends and ArrayList, containing things that extend HashMaps. An arraylist of hashmaps of strings to strings is an almost guaranteed "this needs its own datastructure" signal. Commented Aug 20, 2014 at 5:48

3 Answers 3

1

I suggest you to use MultiMap, similar type of problem can be handle easily with multiMap.

A multimap is like a Map but it can map each key to multiple values.

Go through this doc: Java doc MultiMap

For example:

 MultiMap mhm = new MultiHashMap();
 mhm.put(key, "A");
 mhm.put(key, "B");
 mhm.put(key, "C");
 Collection coll = (Collection) mhm.get(key);

coll will be a collection containing "A", "B", "C".

PS: I think you can write code as per your requirement but If you wish then I will update my answer exactly for your question.

Thanks.

Sign up to request clarification or add additional context in comments.

Comments

0

After many search I go with HashSet.

ArrayList<HashMap<String, String>> counstatelist = new ArrayList<>();
for (int i = 0; i < mylist.size(); i++) {
    HashMap<String, String> tempMap = new HashMap<>();
    tempMap.put("Country", mylist.get(i).get("Country"));
    tempMap.put("State", mylist.get(i).get("State"));
    counstatelist.add(tempMap);
}
HashSet<HashMap<String, String>> hashSet = new HashSet<>();
hashSet.addAll(counstatelist);
counstatelist.clear();
counstatelist.addAll(hashSet);
System.out.println(counstatelist);

Now I got my expected output.

Thanks to all those who tries to help me.

Comments

0

when i = 4
counstatelist = [{State=Tamilnadu, Country=India}, {State=Kerala, Country=India}]
then the inner loop will executed twice and the check of the if condition would be true for the both executions because it compares the (4th element of mylist) with (1st and 2nd elements only of counstatelist) twice and insert the 4th element of myList twice too

So to fix that problem you have to add a break statement after the addition in the inner loop just like that

            for (int j = 0; j < 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);
                    break;
                }
            } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.