Kaveen
Kaveen

Reputation: 129

Sort a list of maps by value in java

I have a list of maps. Which I cannot predict the key of each map. key can be an any two digits number which will change acordingly to the request.But values contains unique values from request to request.Now I need to sort this list of maps according to the value of each key. I've already tried something like bellow,

List<Map<String,String>> listOfMaps = new ArrayList<>();
Map<String,String> map1 = new HashMap<>();
Map<String,String> map2 = new HashMap<>();
map1.put("key1","value1");
map2.put("key2","value1");


listOfMaps.add(map1);
listOfMaps.add(map2);

sort(listOfMaps);

 Collections.sort(listOfMaps, new Comparator<Map<String, String>>() {
        public int compare(final Map<String, String> o1, final Map<String, String> o2) {
            return o1.get("key").compareTo(o2.get("key"));
        }
    });

Since "key" can be differ from map to map, seems I couldnt use the above code. I've also tried these examples.

How to Sort a List of Maps by two Categories?

Sorting list of maps based on a value

Since I'm cannot predict the key, I couldnt find an answer from these post.Can any one help with this?.

Upvotes: 3

Views: 10041

Answers (2)

Darshan Mehta
Darshan Mehta

Reputation: 30819

The below example should work, under the assumption that each map element contains only one entry:

List<Map<String, String>> list = new ArrayList<>();
//Add entries
Collections.sort(list, new Comparator<Map<String, String>>() {
    public int compare(Map<String, String> o1, Map<String, String> o2) {
        Collection<String> values1 = o1.values();
        Collection<String> values2 = o2.values();
        if(!values1.isEmpty() && !values2.isEmpty()){
            return values1.iterator().next().compareTo(values2.iterator().next());
        }else{
            return 0;
        }
    }
});

Upvotes: 5

Paul Boddington
Paul Boddington

Reputation: 37645

While it is occasionally necessary to create a Map with only one key and one value (Collections.singletonMap is used for this), it doesn't look like Map is appropriate for your situation. It makes much more sense to use a custom class with two fields, something like this:

// This is immutable. You may prefer to make fields non-final and add setters.
// You may also want to override equals() and hashCode().
final class StringPair {

    private final String key;
    private final String value;

    StringPair(String key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public String toString() {
        return "[" + key + ", " + value + "]";
    }
}

Then you can do:

List<StringPair> list = new ArrayList<>();
list.add(new StringPair("K2", "V2"));
list.add(new StringPair("K1", "V1"));     
Collections.sort(list, new Comparator<StringPair>() 
    @Override
    public int compare(StringPair o1, StringPair o2) {
        return o1.value.compareTo(o2.value);
    }     
});
System.out.println(list);    // Prints [[K1, V1], [K2, V2]]

Upvotes: 1

Related Questions