java 两个数组查询相同和不同


public class ListTest {
    public static void main(String[] args) {
        List<Integer> a1=new ArrayList<Integer>();
        for (int i = 0; i < 50; i++) {
            a1.add(i+30);
        }
        List<Integer> a2=new ArrayList<Integer>();
        for (int i = 0; i < 50; i++) {
            a2.add(i);
        }

        //查询相同
        Set<Integer> a3=new HashSet<Integer>();
        a3.addAll(a1);
        a3.addAll(a2);

        a3.removeAll(a1);
        //a2特有的
        System.out.println("a2---------------");
        for (Integer in : a3) {
            System.out.print(in+",");
        }
        System.out.println();

        System.out.println("a1 a2 都有------------");

        a2.removeAll(a3);

        for (Integer in : a2) {
            System.out.print(in+",");
        }
        System.out.println();
        a1.removeAll(a2);

        //a2特有的
        System.out.println("a1---------------");
        for (Integer in : a1) {
            System.out.print(in+",");
        }
        System.out.println();


    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM