代码
提交代码
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
public class StreamDemo4 {
static class Person {
private String name;
private int age;
public Person() { }
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
/**
* 创建一个Person的集合
* @return List
*/
public static List<Person> createPeople() {
ArrayList<Person> people = new ArrayList<>();
people.add(new Person("小明", 15));
people.add(new Person("小芳", 20));
people.add(new Person("小李", 18));
people.add(new Person("小付", 23));
people.add(new Person("小付", 23));
people.add(new Person("大飞", 22));
people.add(new Person("大飞", 22));
people.add(new Person("大飞", 22));
return people;
}
public static void main(String[] args) {
List<Person> people = createPeople();
// 创建 Stream 对象
Stream<Person> stream = people.stream();
System.out.println("去重前,集合中元素有:");
stream.forEach(System.out::println);
System.out.println("去重后,集合中元素有:");
// 创建一个新流
Stream<Person> stream1 = people.stream();
// 截断流,并调用终止操作打印集合中元素
stream1.distinct().forEach(System.out::println);
}
}
运行结果