没有覆写实体类方法时,去重失败
实体类:
package com.ahut.common.entity;
import java.util.Objects;
public class BaseBean {
private String name;
private int age;
public BaseBean(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "BaseBean{" +
"name='" + name + '\'' +
", 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;
}
}
去重:
@Test
public void testDistinct() {
List<BaseBean> baseBeanList = new ArrayList<>();
baseBeanList.add(new BaseBean("test1", 11));
baseBeanList.add(new BaseBean("test2", 33));
baseBeanList.add(new BaseBean("test3", 22));
baseBeanList.add(new BaseBean("test1", 11));
baseBeanList.stream().distinct().forEach(System.out::println);
}
结果:
BaseBean{name='test1', age=11}
BaseBean{name='test2', age=33}
BaseBean{name='test3', age=22}
BaseBean{name='test1', age=11}
覆写实体类的equals和hashCode方法,去重成功
实体类:
package com.ahut.common.entity;
import java.util.Objects;
public class BaseBean {
private String name;
private int age;
public BaseBean(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "BaseBean{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseBean baseBean = (BaseBean) o;
return age == baseBean.age &&
name.equals(baseBean.name);
}
@Override
public int hashCode() {
return Objects.hash(name, 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;
}
}
去重:
@Test
public void testDistinct() {
List<BaseBean> baseBeanList = new ArrayList<>();
baseBeanList.add(new BaseBean("test1", 11));
baseBeanList.add(new BaseBean("test2", 33));
baseBeanList.add(new BaseBean("test3", 22));
baseBeanList.add(new BaseBean("test1", 11));
baseBeanList.stream().distinct().forEach(System.out::println);
}
结果:
BaseBean{name='test1', age=11}
BaseBean{name='test2', age=33}
BaseBean{name='test3', age=22}
distinct源码备注:
/**
* Returns a stream consisting of the distinct elements (according to
* {@link Object#equals(Object)}) of this stream.
*
* <p>For ordered streams, the selection of distinct elements is stable
* (for duplicated elements, the element appearing first in the encounter
* order is preserved.) For unordered streams, no stability guarantees
* are made.
*
* <p>This is a <a href="package-summary.html#StreamOps">stateful
* intermediate operation</a>.
*
* @apiNote
* Preserving stability for {@code distinct()} in parallel pipelines is
* relatively expensive (requires that the operation act as a full barrier,
* with substantial buffering overhead), and stability is often not needed.
* Using an unordered stream source (such as {@link #generate(Supplier)})
* or removing the ordering constraint with {@link #unordered()} may result
* in significantly more efficient execution for {@code distinct()} in parallel
* pipelines, if the semantics of your situation permit. If consistency
* with encounter order is required, and you are experiencing poor performance
* or memory utilization with {@code distinct()} in parallel pipelines,
* switching to sequential execution with {@link #sequential()} may improve
* performance.
*
* @return the new stream
*/
Stream<T> distinct();
关于String的去重
因为String类已经覆写了equals和hashCode方法,所以可以去重成功
/**
* Compares this string to the specified object. The result is {@code
* true} if and only if the argument is not {@code null} and is a {@code
* String} object that represents the same sequence of characters as this
* object.
*
* @param anObject
* The object to compare this {@code String} against
*
* @return {@code true} if the given object represents a {@code String}
* equivalent to this string, {@code false} otherwise
*
* @see #compareTo(String)
* @see #equalsIgnoreCase(String)
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
/**
* Returns a hash code for this string. The hash code for a
* {@code String} object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using {@code int} arithmetic, where {@code s[i]} is the
* <i>i</i>th character of the string, {@code n} is the length of
* the string, and {@code ^} indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* @return a hash code value for this object.
*/
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
去重:
@Test
public void testDistinct() {
List<String> strList = new ArrayList<>(4);
strList.add("test1");
strList.add("test2");
strList.add("test3");
strList.add("test1");
strList.stream().distinct().forEach(System.out::println);
}
结果:
test1
test2
test3
文章转自:https://blog.youkuaiyun.com/qq_28988969/article/details/89166376 (大白能)