• java中 得到两个数组中不同的元素 有效


    public static  List compare(T[] t1, T[] t2) {
        List list1 = Arrays.asList(t1);
        List list2 = Arrays.asList(t2);
        //用来保存最后结果的集合
        List list3 = new ArrayList<>();
        for (T t : t2) {
            if (!list1.contains(t)) {
                list3.add(t);
            }
        }
        for (T t:t1) {
            if (!list2.contains(t)) {
                list3.add(t);
            }
        }
        return list3;
    }
    
    public static void main(String[] arg){
        String[] array1 = {"1", "2", "3","12"};
        String[] array2 = {"1", "2", "3", "4","44"};
        List list = compare(array1,array2);
        System.out.println("----两个数组找出不同的元素-----");
        for (String num : list) {
            System.out.print(num+" ");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

      public static void compare(String[] t1, String[] t2) {
                Set<String> set1 = new HashSet<>(Arrays.asList(t1));
                Set<String> set2 = new HashSet<>(Arrays.asList(t2));
                Set<String> set3 = new HashSet<>(set2);
                // set集合有去重特性
                set3.addAll(set1);
                //retainAll():保留包含在指定 collection 中的元素;
                set1.retainAll(set2);
                //	removeAll(); 移除 set 中那些包含在指定 collection 中的元素 ;
                set3.removeAll(set1);
                System.out.println(set3);
            }
    
            public static void main(String[] arg){
                String[] array1 = {"1", "2", "3","12"};
                String[] array2 = {"1", "2", "3", "4","44"};
                compare(array1,array2);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述
    发现不符合业务 这样查询出来 不知道那个要删除 那个要添加

    我的业务是传入数组和数据库对比数据库没有则删除,数据库有则修改,数组多则添加

    public static void compare(String[] t1, String[] t2) {
        //用来保存没有的数据 也就是前端传进来的新数据 新添加
        List addArrayList= new ArrayList(Arrays.asList(t1));
        //用来保存数据库有的数据 但是 前端没有的数据  删除
        List delArrayList = new ArrayList(Arrays.asList(t2));
        for (String s:t1){
            for (String ss:t2){
                if (s.equals(ss)){
                    //相等的话 证明数据已有数据了 不需要删除 也不需要添加  那需要在数组里面把这个条数据删除
                    addArrayList.remove(s);
                    delArrayList.remove(ss);
                }
            }
        }
        System.out.println("t3:"+addArrayList);
        System.out.println("t4:"+delArrayList);
    }
    
    public static void main(String[] arg){
        String[] array1 = {"1", "2", "3","12"};
        String[] array2 = {"1", "2", "3", "4","44"};
        compare(array1,array2);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述
    这样才可以实现我要的数据

  • 相关阅读:
    如何从电脑远程访问 iPhone?
    一文读懂 Redis 缓存系统
    由面试题“Redis是否为单线程”引发的思考【文末送书-23】
    Linux-Nginx安装
    猿创征文|大厂说的 代码门禁如何实现?
    Linux进程状态
    Servlet 常见的API
    全栈工程师需要具备哪些技能?
    内存分配malloc和free
    23种设计模式(十六)策略模式(阁瑞钛伦特软件-九耶实训)
  • 原文地址:https://blog.csdn.net/qq_42862247/article/details/126591478