Want to order chats in ArrayList by timestamp using Comparator, but it's not working and I don't know why

前端 未结 2 1822
别那么骄傲
别那么骄傲 2021-01-21 17:55

I have implemented a chat function in my app and it\'s working fine, only problem is that now I want the chats to be ordered by timestamp, so that the newest ones c

2条回答
  •  無奈伤痛
    2021-01-21 18:18

    You can simply use Java8 feature to compare the element as below:

    List unique = list.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparing(ChatList::getTimestamp))),ArrayList::new));
    unique.forEach((k) -> System.out.println(k.getTimestamp()));
    

提交回复
热议问题