在java 8中合并两个对象列表 [英] Merging two List of objects in java 8

查看:26
本文介绍了在java 8中合并两个对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 类 Parent 有 20 个属性 (attrib1, attrib2 .. attrib20) 及其对应的 getter 和 setter.我还有两个 Parent 对象列表:list1list2.

I have a Java class Parent with 20 attributes (attrib1, attrib2 .. attrib20) and its corresponding getters and setters. Also I have two lists of Parent objects: list1 and list2.

现在我想合并两个列表并避免基于 attrib1attrib2 的重复对象.

Now I want to merge both lists and avoid duplicate objects based on attrib1 and attrib2.

使用 Java 8:

List<Parent> result = Stream.concat(list1.stream(), list2.stream())
                .distinct()
                .collect(Collectors.toList());   

但是我必须在哪个地方指定属性?我应该重写 hashCodeequals 方法吗?

But in which place I have to specify the attributes? Should I override hashCode and equals method?

推荐答案

如果要实现equalshashCode,实现的地方在里面Parent.在该类中添加方法,如

If you want to implement equals and hashCode, the place to do it is inside the class Parent. Within that class add the methods like

    @Override
    public int hashCode() {
        return Objects.hash(getAttrib1(), getAttrib2(), getAttrib3(),
            // …
                            getAttrib19(), getAttrib20());
    }

    @Override
    public boolean equals(Object obj) {
        if(this==obj) return true;
        if(!(obj instanceof Parent)) return false;
        Parent p=(Parent) obj;
        return Objects.equals(getAttrib1(), p.getAttrib1())
            && Objects.equals(getAttrib2(), p.getAttrib2())
            && Objects.equals(getAttrib3(), p.getAttrib3())
            // …
            && Objects.equals(getAttrib19(), p.getAttrib19())
            && Objects.equals(getAttrib20(), p.getAttrib20());
    }

如果您这样做了,在 Stream 上调用的 distinct() 将自动执行正确的操作.

If you did this, distinct() invoked on a Stream<Parent> will automatically do the right thing.

如果您不想(或不能)更改类 Parent,则没有相等的委托机制,但您可以使用 ordering有一个委托机制:

If you don’t want (or can’t) change the class Parent, there is no delegation mechanism for equality, but you may resort to ordering as that has a delegation mechanism:

Comparator<Parent> c=Comparator.comparing(Parent::getAttrib1)
        .thenComparing(Parent::getAttrib2)
        .thenComparing(Parent::getAttrib3)
        // …
        .thenComparing(Parent::getAttrib19)
        .thenComparing(Parent::getAttrib20);

这定义了基于属性的顺序.它要求属性本身的类型具有可比性.如果你有这样的定义,你可以使用它来实现一个 distinct() 的等价物,基于那个 Comparator:

This defines an order based on the properties. It requires that the types of the attributes itself are comparable. If you have such a definition, you can use it to implement the equivalent of a distinct(), based on that Comparator:

List<Parent> result = Stream.concat(list1.stream(), list2.stream())
        .filter(new TreeSet<>(c)::add)
        .collect(Collectors.toList());

还有一个线程安全的变体,以防您想将它与并行流一起使用:

There is also a thread-safe variant, in case you want to use it with parallel streams:

List<Parent> result = Stream.concat(list1.stream(), list2.stream())
        .filter(new ConcurrentSkipListSet<>(c)::add)
        .collect(Collectors.toList());

这篇关于在java 8中合并两个对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆