标签:null turn for class main方法 nal 并集 vat rgs
重写里面的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集合中的交集、差集、并集、去重并集
标签:null turn for class main方法 nal 并集 vat rgs
原文地址:https://www.cnblogs.com/chenyanbin/p/13412669.html