how sort when hashmap value is list of objects by multiple properties java 8

VitalyT :

Suppose I have like :

  Map<String, List<MyState>> map = new HashMap<>();
  map.computeIfAbsent(key, file -> new ArrayList<>()).add(myState);


  map.put("aa",list1..)
  map.put("bb",list2..)
  map.put("cc",list3..)

public class MyState {
    private String state;
    private String date;
}

I want to sort the map values List<MyState> by MyState::date and then by MyState::state

Ousmane D. :

You can do so with:

Comparator<MyState> comparator = Comparator.comparing(MyState::getDate)
                                        .thenComparing(MyState::getState);
map.values()
   .forEach(l -> l.sort(comparator));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=88014&siteId=1