看的这位大神的非常好的博客,大家可以先去看一下: https://blog.csdn.net/john1337/article/details/108027824
使用distinct去重
简单List去重
package com.it.test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List strings = new ArrayList<>();
strings.add("aa");
strings.add("aa");
strings.add("bb");
strings.add("abb");
List collect = strings.stream().distinct().collect(Collectors.toList());
System.out.println(collect.toString());
}
}
结果
简单List去重
package com.it.test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
List integers = new ArrayList<>();
integers.add(1);
integers.add(2);
integers.add(3);
integers.add(4);
integers.add(1);
List collect = integers.stream().distinct().collect(Collectors.toList());
System.out.println(collect.toString());
}
}
结果
List对象去重,把对象去重,一定要重写要去重实体类的hashCode()和equals()方法,
Person.java
package com.it.pojo;
import java.util.Objects;
public class Person {
private String name;
private Integer age;
@Override
public String toString() {
return "Person{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
//重写了hashCode()和equals()方法
if (this == o) return true; //是不是一个对象
if (o == null || getClass() != o.getClass()) return false; //是不是一个类【也可以使用o instanceof Person】
Person person = (Person) o; //是同一个类后强制转换成Person类型的,因为接收参数的时候是Object类型的,不强转的话不能用其中的属性
return getName().equals(person.getName()) &&
getAge().equals(person.getAge());//真正比较是否相等的条件,采用了
}
@Override
public int hashCode() {
return Objects.hash(getName(), getAge());
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
test.java
package com.it.test;
import com.it.pojo.Person;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
Person person1 = new Person();
person1.setAge(18);
person1.setName("18");
Person person2 = new Person();
person2.setAge(19);
person2.setName("19");
Person person3 = new Person();
person3.setAge(19);
person3.setName("19");
List people = new ArrayList<>();
people.add(person1);
people.add(person2);
people.add(person3);
List collect1 = people.stream().distinct().collect(Collectors.toList());
for (Person person : collect1) {
System.out.println(person);
}
}
}
结果
总结
使用Stream流的distinct去重,对实体对象去重,一定要重写实体对象的hashCode()和equals()方法,否则去不了重,因为实际在去重的时候会调用Object.class的hashCode()和equals()方法,Object.class的hashCode()直接返回对象的内存地址值,equals()方法直接使用==来比较两个对象是否相等,所以两个对象(非常亮String对象)只要创建,内存地址值肯定不同,比较不出来,因此一般重写
看的博客说的是stream流的distinct去重的底层使用的hashSet去重,而hashSet实际上是HashMap,所以distinct去重实际上是用了HashMap的key去重
真正去重底层源码没看,下一次看了,补上对Stream.distinct()的理解