java-如何通过多个属性将哈希图值是对象列表时的排序方式设置为Java 8

时间:2018-07-31 08:12:08

标签: java java-8

假设我喜欢:

  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;
}

我想先按List<MyState>然后再按MyState::date对映射值MyState::state进行排序

1 个答案:

答案 0 :(得分:7)

您可以这样做:

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