- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.TreeSet;
- import java.util.stream.Collectors;
- public class Person {
- private Integer id;
- private String firstName;
- private String secondName;
- /**
- *
- */
- public Person() {
- super();
- }
- /**
- * @param id
- * @param firstName
- * @param secondName
- */
- public Person(Integer id, String firstName, String secondName) {
- super();
- this.id = id;
- this.firstName = firstName;
- this.secondName = secondName;
- }
- /*
- * (non-Javadoc)
- * @see java.lang.Object#equals(java.lang.Object)
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- Person other = (Person) obj;
- if (firstName == null) {
- if (other.firstName != null) {
- return false;
- }
- } else if (!firstName.equals(other.firstName)) {
- return false;
- }
- if (id == null) {
- if (other.id != null) {
- return false;
- }
- } else if (!id.equals(other.id)) {
- return false;
- }
- if (secondName == null) {
- if (other.secondName != null) {
- return false;
- }
- } else if (!secondName.equals(other.secondName)) {
- return false;
- }
- return true;
- }
- /**
- * @return the firstName
- */
- public String getFirstName() {
- return firstName;
- }
- /**
- * @return the id
- */
- public Integer getId() {
- return id;
- }
- /**
- * @return the secondName
- */
- public String getSecondName() {
- return secondName;
- }
- /*
- * (non-Javadoc)
- * @see java.lang.Object#hashCode()
- */
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
- result = prime * result + ((id == null) ? 0 : id.hashCode());
- result = prime * result + ((secondName == null) ? 0 : secondName.hashCode());
- return result;
- }
- /**
- * @param firstName
- * the firstName to set
- */
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- /**
- * @param id
- * the id to set
- */
- public void setId(Integer id) {
- this.id = id;
- }
- /**
- * @param secondName
- * the secondName to set
- */
- public void setSecondName(String secondName) {
- this.secondName = secondName;
- }
- /*
- * (non-Javadoc)
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString() {
- return "Person [id=" + id + ", firstName=" + firstName + ", secondName=" + secondName + "]";
- }
- public static void main(String[] args) {
- Person p1 = new Person(1, "wang", "gang");
- Person p2 = new Person(2, "wang", "qiang");
- Person p3 = new Person(3, "wang", "cheng");
- Person p4 = new Person(4, "wang", "qiang");
- Person p5 = new Person(5, "wang", "cheng");
- List<Person> list = new ArrayList<>();
- list.add(p1);
- list.add(p2);
- list.add(p3);
- list.add(p4);
- list.add(p5);
- System.out.println(list);
- /*
- * output:
- * [Person [id=1, firstName=wang, secondName=gang], Person [id=2,
- * firstName=wang, secondName=qiang], Person [id=3, firstName=wang,
- * secondName=cheng]]
- */
- List<Person> distinctList = list.stream()
- .collect(Collectors.collectingAndThen(Collectors.toCollection(
- // 利用 TreeSet 的排序去重构造函数来达到去重元素的目的
- // 根据firstName去重
- () -> new TreeSet<>(Comparator.comparing(Person::getFirstName))), ArrayList::new));
- /*
- * output:
- * [Person [id=1, firstName=wang, secondName=gang]]
- */
- System.out.println(distinctList);
- List<Person> distinctList1 = list.stream().collect(Collectors
- .collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> {
- // 根据firstName和secondName进行去重
- return o.getFirstName() + "," + o.getSecondName();
- }))), ArrayList::new));
- /*
- * output:
- * [Person [id=3, firstName=wang, secondName=cheng], Person [id=1,
- * firstName=wang, secondName=gang], Person [id=2, firstName=wang,
- * secondName=qiang]]
- */
- System.out.println(distinctList1);
- TreeSet<Person> set = new TreeSet<>(Comparator.comparing(o -> {
- // 根据firstName和secondName进行去重
- return o.getFirstName() + "," + o.getSecondName();
- }));
- set.addAll(list);
- /*
- * output:
- * [Person [id=3, firstName=wang, secondName=cheng], Person [id=1,
- * firstName=wang, secondName=gang], Person [id=2, firstName=wang,
- * secondName=qiang]]
- */
- System.out.println(set);
- }
- }