User类
package com.gao.JDK8.Stream流; import lombok.Data; import java.util.Date; @Data public class User { private String name; private String pass; private Date time; public User(String name, String pass, Date time) { this.name = name; this.pass = pass; this.time = time; } }
实现-去重
package com.gao.JDK8.Stream流; import java.util.*; import java.util.stream.Collectors; public class G01 { public static void main(String[] args) { //Stream按对象某属性去重的方案 User user1 = new User("高","123456",new Date()); User user11 = new User("高","123456",new Date()); User user2 = new User("王", "123456", new Date()); User user22 = new User("王", "123456", new Date()); User user3 = new User("g", "123", null); List<User> userList = Arrays.asList(user1, user2, user3); ArrayList<User> collect1 = userList.stream().collect( // 将集合先放到 treeSet 集合然后将他们转换成数组 Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(user -> user.getName()))) , ArrayList::new) ); collect1.forEach(System.out::println);
} }
实现--结果:
标签:String,Stream,---,User,pass,Date,new,属性 From: https://www.cnblogs.com/Life-QX/p/16867564.html