java8 根据类的某些属性进行去重操作

  1. import java.util.ArrayList;  
  2. import java.util.Comparator;  
  3. import java.util.List;  
  4. import java.util.TreeSet;  
  5. import java.util.stream.Collectors;  
  6.   
  7. public class Person {  
  8.     private Integer id;  
  9.     private String firstName;  
  10.     private String secondName;  
  11.   
  12.     /** 
  13.      * 
  14.      */  
  15.     public Person() {  
  16.         super();  
  17.     }  
  18.   
  19.     /** 
  20.      * @param id 
  21.      * @param firstName 
  22.      * @param secondName 
  23.      */  
  24.     public Person(Integer id, String firstName, String secondName) {  
  25.         super();  
  26.         this.id = id;  
  27.         this.firstName = firstName;  
  28.         this.secondName = secondName;  
  29.     }  
  30.   
  31.     /* 
  32.      * (non-Javadoc) 
  33.      * @see java.lang.Object#equals(java.lang.Object) 
  34.      */  
  35.     @Override  
  36.     public boolean equals(Object obj) {  
  37.         if (this == obj) {  
  38.             return true;  
  39.         }  
  40.         if (obj == null) {  
  41.             return false;  
  42.         }  
  43.         if (getClass() != obj.getClass()) {  
  44.             return false;  
  45.         }  
  46.         Person other = (Person) obj;  
  47.         if (firstName == null) {  
  48.             if (other.firstName != null) {  
  49.                 return false;  
  50.             }  
  51.         } else if (!firstName.equals(other.firstName)) {  
  52.             return false;  
  53.         }  
  54.         if (id == null) {  
  55.             if (other.id != null) {  
  56.                 return false;  
  57.             }  
  58.         } else if (!id.equals(other.id)) {  
  59.             return false;  
  60.         }  
  61.         if (secondName == null) {  
  62.             if (other.secondName != null) {  
  63.                 return false;  
  64.             }  
  65.         } else if (!secondName.equals(other.secondName)) {  
  66.             return false;  
  67.         }  
  68.         return true;  
  69.     }  
  70.   
  71.     /** 
  72.      * @return the firstName 
  73.      */  
  74.     public String getFirstName() {  
  75.         return firstName;  
  76.     }  
  77.   
  78.     /** 
  79.      * @return the id 
  80.      */  
  81.     public Integer getId() {  
  82.         return id;  
  83.     }  
  84.   
  85.     /** 
  86.      * @return the secondName 
  87.      */  
  88.     public String getSecondName() {  
  89.         return secondName;  
  90.     }  
  91.   
  92.     /* 
  93.      * (non-Javadoc) 
  94.      * @see java.lang.Object#hashCode() 
  95.      */  
  96.     @Override  
  97.     public int hashCode() {  
  98.         final int prime = 31;  
  99.         int result = 1;  
  100.         result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());  
  101.         result = prime * result + ((id == null) ? 0 : id.hashCode());  
  102.         result = prime * result + ((secondName == null) ? 0 : secondName.hashCode());  
  103.         return result;  
  104.     }  
  105.   
  106.     /** 
  107.      * @param firstName 
  108.      *            the firstName to set 
  109.      */  
  110.     public void setFirstName(String firstName) {  
  111.         this.firstName = firstName;  
  112.     }  
  113.   
  114.     /** 
  115.      * @param id 
  116.      *            the id to set 
  117.      */  
  118.     public void setId(Integer id) {  
  119.         this.id = id;  
  120.     }  
  121.   
  122.     /** 
  123.      * @param secondName 
  124.      *            the secondName to set 
  125.      */  
  126.     public void setSecondName(String secondName) {  
  127.         this.secondName = secondName;  
  128.     }  
  129.   
  130.     /* 
  131.      * (non-Javadoc) 
  132.      * @see java.lang.Object#toString() 
  133.      */  
  134.     @Override  
  135.     public String toString() {  
  136.         return "Person [id=" + id + ", firstName=" + firstName + ", secondName=" + secondName + "]";  
  137.     }  
  138.   
  139.     public static void main(String[] args) {  
  140.         Person p1 = new Person(1, "wang", "gang");  
  141.         Person p2 = new Person(2, "wang", "qiang");  
  142.         Person p3 = new Person(3, "wang", "cheng");  
  143.         Person p4 = new Person(4, "wang", "qiang");  
  144.         Person p5 = new Person(5, "wang", "cheng");  
  145.         List<Person> list = new ArrayList<>();  
  146.         list.add(p1);  
  147.         list.add(p2);  
  148.         list.add(p3);  
  149.         list.add(p4);  
  150.         list.add(p5);  
  151.         System.out.println(list);  
  152.         /* 
  153.          * output: 
  154.          * [Person [id=1, firstName=wang, secondName=gang], Person [id=2, 
  155.          * firstName=wang, secondName=qiang], Person [id=3, firstName=wang, 
  156.          * secondName=cheng]] 
  157.          */  
  158.         List<Person> distinctList = list.stream()  
  159.                 .collect(Collectors.collectingAndThen(Collectors.toCollection(  
  160.                         // 利用 TreeSet 的排序去重构造函数来达到去重元素的目的  
  161.                         // 根据firstName去重  
  162.                         () -> new TreeSet<>(Comparator.comparing(Person::getFirstName))), ArrayList::new));  
  163.         /* 
  164.          * output: 
  165.          * [Person [id=1, firstName=wang, secondName=gang]] 
  166.          */  
  167.         System.out.println(distinctList);  
  168.   
  169.         List<Person> distinctList1 = list.stream().collect(Collectors  
  170.                 .collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> {  
  171.                     // 根据firstName和secondName进行去重  
  172.                     return o.getFirstName() + "," + o.getSecondName();  
  173.                 }))), ArrayList::new));  
  174.         /* 
  175.          * output: 
  176.          * [Person [id=3, firstName=wang, secondName=cheng], Person [id=1, 
  177.          * firstName=wang, secondName=gang], Person [id=2, firstName=wang, 
  178.          * secondName=qiang]] 
  179.          */  
  180.         System.out.println(distinctList1);  
  181.   
  182.         TreeSet<Person> set = new TreeSet<>(Comparator.comparing(o -> {  
  183.             // 根据firstName和secondName进行去重  
  184.             return o.getFirstName() + "," + o.getSecondName();  
  185.         }));  
  186.         set.addAll(list);  
  187.         /* 
  188.          * output: 
  189.          * [Person [id=3, firstName=wang, secondName=cheng], Person [id=1, 
  190.          * firstName=wang, secondName=gang], Person [id=2, firstName=wang, 
  191.          * secondName=qiang]] 
  192.          */  
  193.         System.out.println(set);  
  194.     }  
  195. }  
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值