JavaSE 计算2个List集合中的交集差集并集去重并集

Posted chenyanbin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaSE 计算2个List集合中的交集差集并集去重并集相关的知识,希望对你有一定的参考价值。

VideoOrder.java

重写里面的equals和hashCode方法

class VideoOrder {
    private int price;
    private String title;

    public VideoOrder(String title, int price) {
        this.title = title;
        this.price = price;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        VideoOrder that = (VideoOrder) o;
        return price == that.price && Objects.equals(title, that.title);
    }

    @Override
    public int hashCode() {
        return Objects.hash(price, title);
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "VideoOrder{" + "price=" + price + ", title=‘" + title + ‘‘‘ + ‘}‘;
    }
}

main方法

    public static void main(String[] args) {
        List<VideoOrder> videoOrders1 = new ArrayList<>();
        videoOrders1.add(new VideoOrder("a课程", 22));
        videoOrders1.add(new VideoOrder("w课程", 200));
        videoOrders1.add(new VideoOrder("c课程", 100));
        videoOrders1.add(new VideoOrder("d课程", 33));
        videoOrders1.add(new VideoOrder("f课程", 1));
        List<VideoOrder> videoOrders2 = new ArrayList<>();
        videoOrders2.add(new VideoOrder("a课程", 22));
        videoOrders2.add(new VideoOrder("b课程", 18));
        videoOrders2.add(new VideoOrder("d课程", 33));
        videoOrders2.add(new VideoOrder("f课程", 1));
        videoOrders2.add(new VideoOrder("z课程", 22));
        // 交集,方式一
        // videoOrders1.retainAll(videoOrders2);
        // System.out.println(videoOrders1);
        //方式二
        List<VideoOrder> intersectionList = new ArrayList<>();
        for (VideoOrder videoOrder : videoOrders1) {
            if (videoOrders2.contains(videoOrder)) {
                intersectionList.add(videoOrder);
            }
        }
        System.out.println("交集=" + intersectionList);
        // videoOrders1差集,方式一
        // videoOrders1.removeAll(videoOrders2);
        // System.out.println(videoOrders1);
        //方式二
        List<VideoOrder> diffList1 = new ArrayList<>();
        for (VideoOrder videoOrder : videoOrders1) {
            if (!videoOrders2.contains(videoOrder)) {
                diffList1.add(videoOrder);
            }
        }
        System.out.println("订单1差集=" + diffList1);
        // videoOrders2差集
        // videoOrders2.removeAll(videoOrders1);
        // System.out.println(videoOrders2);
        List<VideoOrder> diffList2 = new ArrayList<>();
        for (VideoOrder videoOrder : videoOrders2) {
            if (!videoOrders1.contains(videoOrder)) {
                diffList2.add(videoOrder);
            }
        }
        System.out.println("订单2差集=" + diffList2);
        // 并集
         videoOrders1.addAll(videoOrders2);
         System.out.println(videoOrders1);
        // 去重并集
        // Set<VideoOrder> set = new HashSet<>(videoOrders1);
        // System.out.println(set);
    }

 

 

 

以上是关于JavaSE 计算2个List集合中的交集差集并集去重并集的主要内容,如果未能解决你的问题,请参考以下文章

求2个list的差集

java找到两个list的交集并集差集

Java使用不同方式获取两个集合List的交集补集并集(相加)差集(相减)

PHP数组的几个操作,求并集,交集,差集,数组

java8两个List集合取交集并集差集去重并集

List的 并集交集差集操作