Java 使用 Stream 去重
根据对象单个属性进行去重
在项目中可以会获取到一个集合,集合中有很多重复的数据,但是总感觉转成Set去重不太体面,那就可以试试使用 Stream 来实现根据对象的属性进行去重
将集合转为Stream流后通过Collectors.toMap 进行去重
-
UserEntity::getName
指定根据名字去重 -
user -> user
执行后要转换的类型(因为我们不需要转换所以不处理) -
(u1, u2) -> u1)
然后存在多个时我们保留第一个对象
@Test
public void propertyDistinct(){
UserEntity a1 = new UserEntity().setId(1).setStatus(1).setName("A1");
UserEntity a2 = new UserEntity().setId(2).setStatus(2).setName("A2");
UserEntity a3 = new UserEntity().setId(3).setStatus(3).setName("A3");
UserEntity a4 = new UserEntity().setId(4).setStatus(4).setName("A1");
UserEntity a5 = new UserEntity().setId(5).setStatus(3).setName("A5");
UserEntity a6 = new UserEntity().setId(6).setStatus(2).setName("A3");
UserEntity a7 = new UserEntity().setId(7).setStatus(4).setName("A2");
List<UserEntity> userList = Arrays.asList(a1, a2, a3, a4, a5, a6, a7);
List<UserEntity> list = userList.stream()
.collect(Collectors.toMap(UserEntity::getName, user -> user, (u1, u2) -> u1))
.values()
.stream()
.collect(Collectors.toList());
list.forEach(System.out::println);
}
实现结果:
UserEntity(id=1, code=null, status=1, name=A1, sex=null, birthday=null, pingList=null)
UserEntity(id=2, code=null, status=2, name=A2, sex=null, birthday=null, pingList=null)
UserEntity(id=3, code=null, status=3, name=A3, sex=null, birthday=null, pingList=null)
UserEntity(id=5, code=null, status=3, name=A5, sex=null, birthday=null, pingList=null)
根据对象多个属性进行去重
根据集合对象中的 Id+“-”+Name 进行去重
@Test
public void propertyDistinct(){
UserEntity a1 = new UserEntity().setId(1).setStatus(1).setName("A1");
UserEntity a2 = new UserEntity().setId(2).setStatus(2).setName("A2");
UserEntity a3 = new UserEntity().setId(3).setStatus(3).setName("A3");
UserEntity a4 = new UserEntity().setId(1).setStatus(4).setName("A1");
UserEntity a5 = new UserEntity().setId(5).setStatus(3).setName("A5");
UserEntity a6 = new UserEntity().setId(6).setStatus(2).setName("A3");
UserEntity a7 = new UserEntity().setId(7).setStatus(4).setName("A2");
List<UserEntity> userList = Arrays.asList(a1, a2, a3, a4, a5, a6, a7);
List<UserEntity> uniqueList = userList.stream()
.collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getId() + "-" + o.getName()))),
ArrayList::new
));
uniqueList.forEach(System.out::println);
}
实现结果:
UserEntity(id=1, code=null, status=1, name=A1, sex=null, birthday=null, pingList=null)
UserEntity(id=2, code=null, status=2, name=A2, sex=null, birthday=null, pingList=null)
UserEntity(id=3, code=null, status=3, name=A3, sex=null, birthday=null, pingList=null)
UserEntity(id=5, code=null, status=3, name=A5, sex=null, birthday=null, pingList=null)
UserEntity(id=6, code=null, status=2, name=A3, sex=null, birthday=null, pingList=null)
UserEntity(id=7, code=null, status=4, name=A2, sex=null, birthday=null, pingList=null)
根据对象所有属性进行去重
使用的 stream 的 distinct() 方法必须对象属性都相同
@Test
public void propertyDistinct(){
UserEntity a1 = new UserEntity().setId(1).setStatus(1).setName("A1");
UserEntity a2 = new UserEntity().setId(2).setStatus(2).setName("A2");
UserEntity a3 = new UserEntity().setId(3).setStatus(3).setName("A3");
UserEntity a4 = new UserEntity().setId(1).setStatus(1).setName("A1");
UserEntity a5 = new UserEntity().setId(5).setStatus(3).setName("A5");
UserEntity a6 = new UserEntity().setId(3).setStatus(3).setName("A3");
UserEntity a7 = new UserEntity().setId(7).setStatus(4).setName("A2");
List<UserEntity> userList = Arrays.asList(a1, a2, a3, a4, a5, a6, a7);
List<UserEntity> uniqueList = userList.stream().distinct().collect(Collectors.toList());
uniqueList.forEach(System.out::println);
}
实现结果:
UserEntity(id=1, code=null, status=1, name=A1, sex=null, birthday=null, pingList=null)
UserEntity(id=2, code=null, status=2, name=A2, sex=null, birthday=null, pingList=null)
UserEntity(id=3, code=null, status=3, name=A3, sex=null, birthday=null, pingList=null)
UserEntity(id=5, code=null, status=3, name=A5, sex=null, birthday=null, pingList=null)
UserEntity(id=7, code=null, status=4, name=A2, sex=null, birthday=null, pingList=null)