list实体类属性去重取新

使用java8 根据list实体类(例如姓名)属性去重如何去前面的而不是后面的
具体代码
 List<User> list = Arrays.asList(
                new User(1,"张三","15810067544",23),
                new User(2,"李四","15810067555",36),
                new User(3,"张三","15810067545",24),
                new User(4,"赵六","15810067555",67)
        );
        List<User> result = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new
        ));
        System.out.println(JSON.toJSON(result));

实体类

@Data
public class User {
    private String name;
    private int age ;
    private String no;
    private int id;

    public User( int id,String name, String no, int age) {
        this.name = name;
        this.age = age;
        this.no = no;
        this.id = id;
    }
}
运行结果:
{"no":"15810067544","name":"张三","id":1,"age":23},{"no":"15810067555","name":"李四","id":2,"age":36},{"no":"15810067555","name":"赵六","id":4,"age":67}

我想要达到的结果:
[,{"no":"15810067555","name":"李四","id":2,"age":36},{"no":"15810067545","name":"张三","id":3,"age":24},{"no":"15810067555","name":"赵六","id":4,"age":67}]

想用最新的张三信息

貌似可以 Collections.reverse(list) 反着排列下list然后再用这个 不知道有没有更简便的方法


        List<User> result = list.stream().collect(Collectors.toMap(User::getName, Function.identity(), (a, b) -> b)).entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());


换个思路

  1. 通过name分组和age排序
  2. 对分组排序后的数据进行循环把需要保留的数据打一个标记
  3. 对整个列表进行迭代,根据标记删除不需要的数据