简体   繁体   中英

Sort out a List<Map> in Java 7

I have a data structure of type List<Map<String, String>> and I am looking to sort out all the Map values inside the List as per the value. For example - the list when iterated gives a Map which consists of two key value pairs.

Map1--> Key1:  abc and key2: Sometext2
Map2 --> Key1: cbd and key2: Sometext2
Map3 --> Key1: bcd and key2: Sometext2
.
.
.
.

and so on. I am looking to sort alphabetically the data structure by the values mapped to key1 so it should look like:

Map1--> Key1:  abc and key2:Sometext2.
Map3 --> Key1: bcd and key2: Sometext2
Map2 --> Key1: cbd and key2: Sometext2
.
.
.
.

I am using Java 7. Any help is highly appreciated.

Thanks

Based on your example, your Map is simply a couple (String, String) .

So we can do:

public class MyCouple {
    private String first;
    private String second;
    // + constructors, getters, setters
}

List<MyCouple> couples = // ...
Collections.sort(couples, new Comparator<MyCouple>(){
    @Override
    public int compare(MyCouple c1, MyCouple c2) {
        return c1.getSecond().compareTo(c2.getSecond()); // sort by second
    }
});

You could use a wrapper class for the map and then get the iterator for your list. Something like this:

class MapWrapper implements Comparable<Map> {
    private Map<String, String> map;

    public MapWrapper (Map<String, String> map) {
        this.map = map;
    }

    public int compareTo() {
        do stuff
    }
}

I had a similar requirement and I developed a comparable map. Class declaration is:

public class ComparableMap<K,V> extends TreeMap<K,V> implements Comparable<ComparableMap<K,V>> {
    @Override
    public int compareTo(ComparableMap<K,V> o) {...}
}

Since TreeMap iterator returns keys sorted, to compare 2 maps, you iterate through the keys, comparing each key until either keys are not equal, or for equal keys, values are not equal, returning the appropriate result depending on which is less. When reach end of one keyset, if other has keys, then they are also not equal, and you would return appropriate compare result.

Finally, you can then use Collections.sort() to sort the list of maps, since the compareTo method will be used to determine the sort order.

Hope this give you some ideas.

If you want individual keys across all maps sorted, you'll probably have generate a merged map.

Try this

static <T> Map<String, T> makeMap(String k1, T v1, String k2, T v2) {
    Map<String, T> map = new LinkedHashMap<>();
    map.put(k1, v1);
    map.put(k2, v2);
    return map;
}

public static void main(String[] args) {
    List<Map<String, Integer>> list = Arrays.asList(
        makeMap("abc", 1, "Sometext2", 1),
        makeMap("cde", 2, "Sometext2", 2),
        makeMap("bcd", 3, "Sometext2", 3)
    );
    System.out.println("before: " + list);
    Collections.sort(list, new Comparator<Map<String, Integer>>() {

        @Override
        public int compare(Map<String, Integer> o1, Map<String, Integer> o2) {
            return o1.keySet().iterator().next()
                .compareTo(o2.keySet().iterator().next()); 
        }

    });
    System.out.println("after:  " + list);
}

Results:

before: [{abc=1, Sometext2=1}, {cde=2, Sometext2=2}, {bcd=3, Sometext2=3}]
after:  [{abc=1, Sometext2=1}, {bcd=3, Sometext2=3}, {cde=2, Sometext2=2}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM