java8 stream api对集合中实体类进行去重(distinct),记得覆写实体类的equals和hashCode方法

博客主要讲述实体类去重相关内容。未覆写实体类的equals和hashCode方法时去重失败,覆写后去重成功。还提到String类因已覆写这两个方法,去重能成功,最后给出文章转载来源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

没有覆写实体类方法时,去重失败
实体类:

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 (大白能)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值