Person
包装类
List people = Arrays.asList(new Person("Kowalski"),
new Person("Nowak"),
new Person("Big"),
new Person("Kowalski"));
List distPeople = people.stream()
.map(Wrapper::new)
.distinct()
.map(Wrapper::unwrap)
.collect(Collectors.toList());
distinct()
返回由不同元素组成的流(根据
Wrapper
那不管用(我用两个
public class Wrapper
{
private final Person person;
Wrapper(Person p)
{
person = p;
}
public Person unwrap()
{
return person;
}
public boolean equals(Object other)
{
if(other instanceof Wrapper)
return ((Wrapper) other).person.getName().equals(person.getName());
else
return false;
}
}
实施
添加此项后类工作:
@Override
public int hashCode()
{
return person.getName().hashCode();
}
hashCode()
包装
作品?