Oracle Monkey
Oracle Monkey

Reputation: 85

Compare Map values and sort

This is my class

public class Person {

int id;
int salary;
Map<String,List<String>> house;
    //getter and setters omitted for brevity

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    Map<String,List<String>> map =new HashMap<>();

    map.put("KEY_1",Arrays.asList("3")  )  ;
    map.put("KEY_2",Arrays.asList("House_1") )  ;
    map.put("KEY_3",Arrays.asList("House_13")  )  ;



    Map<String,List<String>> map2 =new HashMap<>();

    map2.put("KEY_1",Arrays.asList("1") )  ;
    map2.put("KEY_2",Arrays.asList("House_2") )  ;
    map2.put("KEY_3",Arrays.asList("House_22") )  ;


    Map<String,List<String>> map3 =new HashMap<>();

    map3.put("KEY_1",Arrays.asList("2") )  ;
    map3.put("KEY_2",Arrays.asList("House_3") )  ;
    map3.put("KEY_3",Arrays.asList("House_33") )  ;


    Person p1 = new Person(1,1000, map  );

    Person p2 = new Person(2,2000, map2  );

    Person p3 = new Person(3,3000, map3 );


    List<Person> personList = new  ArrayList();
    personList.add(p1);
    personList.add(p2);
    personList.add(p3);



    for (Person p : personList)
    {

        for(Entry<String, List<String>>  personMap :  p.getHouse().entrySet()  )

        {

            System.out.println(  "value of key is -->" +  personMap.getKey() +   " value is " +  personMap.getValue()  );

        }
    }

}
}

My requirement is the sort the data first on the basis KEY_1 value then if the value of KEY_2 ? How can i write a comparator or do comparison for this?

Current O/p is

value of key is -->KEY_1 value is [3]
value of key is -->KEY_3 value is [House_13]
value of key is -->KEY_2 value is [House_1]
value of key is -->KEY_1 value is [1]
value of key is -->KEY_3 value is [House_22]
value of key is -->KEY_2 value is [House_2]
value of key is -->KEY_1 value is [2]
value of key is -->KEY_3 value is [House_33]
value of key is -->KEY_2 value is [House_3]

But my expected output is

value of key is -->KEY_1 value is [3] value of key is -->KEY_3 value is [House_13] value of key is -->KEY_2 value is [House_1]

value of key is -->KEY_1 value is [2] value of key is -->KEY_3 value is [House_33] value of key is -->KEY_2 value is [House_3]

value of key is -->KEY_1 value is [1] value of key is -->KEY_3 value is [House_22] value of key is -->KEY_2 value is [House_2]

i.e if object having key_1 has highest value it should come first and then we need to sort it on the basis of KEY_2 too.

Upvotes: 0

Views: 101

Answers (1)

Konrad H&#246;ffner
Konrad H&#246;ffner

Reputation: 12207

Your example isn't complete, please always post a Minimal, Reproducible Example and provide a clear problem description. For example, your getElements() method is not included and messages is not defined.

However as far as I can tell from the incomplete example, you want to traverse a map in the lexical order of string keys, ascending.

For this you have to know that the Map.entrySet() returns an instance of class Set and you use a for-each loop, which uses its iterator and the iterator of a Set does not guarantee any specific iteration order of the entries.

However you can use the class SortedMap:

A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time. This order is reflected when iterating over the sorted map's collection views (returned by the entrySet, keySet and values methods). Several additional operations are provided to take advantage of the ordering. (This interface is the map analogue of SortedSet.)

If instead you want to want to traverse the map based on the values and not the keys, please refer to the existing answer Sort a Map<Key, Value> by values.

Upvotes: 1

Related Questions