public class Message { private int id; private User sender; private User receiver; private String text; private Date senddate; .. }
You can use overloaded groupingBy method and pass TreeMap as Supplier:
TreeMap
TreeMap> map = list .stream() .collect(Collectors.groupingBy(Message::getSender, () -> new TreeMap<>(new Usercomparator()), toList()));