Java 8 Stream API toMap converting to TreeMap

后端 未结 2 1634
耶瑟儿~
耶瑟儿~ 2021-02-05 17:35
public class Message {
    private int id;
    private User sender;
    private User receiver;
    private String text;   
    private Date senddate;
..
}
2条回答
  •  逝去的感伤
    2021-02-05 18:21

    You can use overloaded groupingBy method and pass TreeMap as Supplier:

    TreeMap> map = list
                .stream()
                .collect(Collectors.groupingBy(Message::getSender,
                        () -> new TreeMap<>(new Usercomparator()), toList()));
    

提交回复
热议问题