看的這位大神的非常好的博客,大家可以先去看一下: https://blog.csdn.net/john1337/article/details/108027824
- 使用distinct去重
- 簡單List<String>去重
-
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<String> strings = new ArrayList<>(); strings.add("aa"); strings.add("aa"); strings.add("bb"); strings.add("abb"); List<String> collect = strings.stream().distinct().collect(Collectors.toList()); System.out.println(collect.toString()); } }結果
-
-
- 簡單List<Integer>去重
-
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<Integer> integers = new ArrayList<>(); integers.add(1); integers.add(2); integers.add(3); integers.add(4); integers.add(1); List<Integer> collect = integers.stream().distinct().collect(Collectors.toList()); System.out.println(collect.toString()); } }結果
-
-
- List<T>物件去重,把物件去重,一定要重寫要去重物體類的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<Person> people = new ArrayList<>(); people.add(person1); people.add(person2); people.add(person3); List<Person> collect1 = people.stream().distinct().collect(Collectors.toList()); for (Person person : collect1) { System.out.println(person); } } }結果
-
-
-
- Person.java
- 簡單List<String>去重
總結
使用Stream流的distinct去重,對物體物件去重,一定要重寫物體物件的hashCode()和equals()方法,否則去不了重,因為實際在去重的時候會呼叫Object.class的hashCode()和equals()方法,Object.class的hashCode()直接回傳物件的記憶體地址值,equals()方法直接使用==來比較兩個物件是否相等,所以兩個物件(非常亮String物件)只要創建,記憶體地址值肯定不同,比較不出來,因此一般重寫
看的博客說的是stream流的distinct去重的底層使用的hashSet去重,而hashSet實際上是HashMap,所以distinct去重實際上是用了HashMap的key去重
真正去重底層原始碼沒看,下一次看了,補上對Stream.distinct()的理解
使用Set對List<T>去重的另一篇博客詳情請看:https://blog.csdn.net/dsl59741/article/details/110010546
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/243301.html
標籤:java
下一篇:JAVA學習



