Java 8中如何对list对象去重(单个属性,多个属性)呢?
									
下文笔者讲述java8中对list集合进行去重的操作分享,如下所示
				 
				
实现思路:
    使用stream()
	然后后借助Collectors.collectingAndThen
	方法即可实现对list集合进行去重的操作分享
例:
单属性: List<User> ArrayList = lists.stream().collect( Collectors.collectingAndThen(Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing(User::getName))),ArrayList::new)); 多属性: List<User> arrayList = lists.stream().collect( Collectors.collectingAndThen(Collectors.toCollection( () -> new TreeSet<>(Comparator.comparing( o -> o.getName() + ";" + o.getAge() + ";" + o.getScore() + ";" + o.getRemarks() + ";" + o.getId()) )), ArrayList::new));
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

			
               
               
               
               
          
