Java 对比两个list 找出重复的 和不重复的

使用场景:

list 可以对比两个list 那些是重复的 那些是不重复的,直接把 500 个船名放到list 里面 然后再把 指挥系统查出来的400个船名放到新的list 里面 然后掉一个方法能对比出来两个list 交际 差集 并集

java 复制代码
public static List<String> findNonIntersection(List<String> list1, List<String> list2) {
    List<String> difference = new ArrayList<>(list1);
    difference.removeAll(list2);//移除list1里面相同元素 得到差集
    return difference;
}

public static void main(String[] args) {
    List<String> list1 = Arrays.asList("1", "2", "3", "4", "5");
    List<String> list2 = Arrays.asList("1", "2", "4");

    List<String> nonIntersection = findNonIntersection(list1, list2);
    System.out.println("没有交集的部分:" + nonIntersection);
}

实际应用:

java 复制代码
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ShipNameComparison {

    public static void main(String[] args) {
        // 模拟500个船名
        List<String> shipNamesList1 = new ArrayList<>();
        for (int i = 1; i <= 500; i++) {
            shipNamesList1.add("Ship" + i);
        }

        // 模拟400个船名
        List<String> shipNamesList2 = new ArrayList<>();
        for (int i = 250; i <= 650; i++) {
            shipNamesList2.add("Ship" + i);
        }

        // 获取交集、差集和并集
        Set<String> intersection = getIntersection(shipNamesList1, shipNamesList2);
        Set<String> difference1 = getDifference(shipNamesList1, shipNamesList2);
        Set<String> difference2 = getDifference(shipNamesList2, shipNamesList1);
        Set<String> union = getUnion(shipNamesList1, shipNamesList2);

        // 输出结果
        System.out.println("交集: " + intersection);
        System.out.println("船名在List1但不在List2的差集: " + difference1);
        System.out.println("船名在List2但不在List1的差集: " + difference2);
        System.out.println("并集: " + union);
    }
    
    // 获取交集
    public static Set<String> getIntersection(List<String> list1, List<String> list2) {
        Set<String> set1 = new HashSet<>(list1);
        Set<String> set2 = new HashSet<>(list2);
        set1.retainAll(set2); // 保留set1中也存在于set2中的元素
        return set1;
    }

    // 获取差集
    public static Set<String> getDifference(List<String> list1, List<String> list2) {
        Set<String> set1 = new HashSet<>(list1);
        Set<String> set2 = new HashSet<>(list2);
        set1.removeAll(set2); // 移除set1中也存在于set2中的元素
        return set1;
    }

    // 获取并集
    public static Set<String> getUnion(List<String> list1, List<String> list2) {
        Set<String> unionSet = new HashSet<>(list1);
        unionSet.addAll(list2); // 添加list2的所有元素
        return unionSet;
    }
}
相关推荐
love530love2 分钟前
2026年终极防坑指南:基于 EPGF 架构彻底“本地化” UV 环境与工具
人工智能·windows·python·架构·devops·uv·epgf
北城以北888813 分钟前
虚拟机安装JDK,Tomcat,部署项目
java·开发语言·tomcat
终将老去的穷苦程序员20 分钟前
基于Android Studio开发的安卓图书借阅管理系统
java·sqlite·android studio·android-studio
虾壳云官方21 分钟前
【本地 AI 自动化最新工具】 OpenClaw 2.7.9 Windows 完整部署教程(包含安装包)
人工智能·windows·openclaw·openclaw安装·openclaw一键部署
lzjava202423 分钟前
Python的数据结构,推导式、迭代器和生成器
数据结构·windows·python
接着奏乐接着舞1 小时前
springboot mp mybatis plaus
windows·spring boot·mybatis
技术小结-李爽1 小时前
【工具】Maven的使用
java·maven
sou_time1 小时前
从 0 到 商用:AI Agent x SKILL x MCP 全栈实战教程:L2 高等篇:MCP 协议 + Spring AI + Agent 编排
java·人工智能·spring
冷小鱼1 小时前
高级研发编码习惯:从规范到艺术,再到AI+时代的人机协同
java·开发语言·python·编码习惯
齐 飞1 小时前
JDK21虚拟线程
java·后端