【问题标题】:Getting unique values from two arraylists with Collections使用 Collections 从两个数组列表中获取唯一值
【发布时间】:2019-08-27 02:04:22
【问题描述】:

在堆栈上查看了几个答案,试图在这个Simple way to compare 2 ArrayLists 的帮助下做到这一点,但无法弄清楚似乎是什么问题。为了总结不可见的代码,我创建了两个包含 4 个文件名的数组列表。现在我试图获取第三个数组列表,它将只包含这两个数组列表中的唯一值。 示例:第一个数组列表 - 一、二、三、四 第二个数组列表 - 一、三、五、七 3rd arraylist - 二,四,五,七(解决方案arraylist) 代码如下:

Collection<String> filesFromDir = new 
ArrayList(Arrays.asList(listOfFilenamesWithNoExtension));

        Collection<String> filesFromDB = new ArrayList(Arrays.asList(listOfFilesDB));

        List<String> listDir = new ArrayList<String>(filesFromDir);
        List<String> listDB = new ArrayList<String>(filesFromDB);

        listDir.removeAll(listDB);
        listDB.removeAll(listDir);

        System.out.println("Unique values: ");
        System.out.println(listDir);
        System.out.println(listDB);

【问题讨论】:

    标签: java


    【解决方案1】:

    你好,对不起,我的初学者代码在这里,但你能不能制作第三个数组列表,循环第一个,然后将所有元素添加到第一个数组列表中。然后遍历第二个列表,如果不存在则添加第三个数组列表中的元素,如果存在则删除。 看看下面的代码,希望对你有帮助

    public void sort(ArrayList<String> one, ArrayList<String> two){
            ArrayList<String> three = new ArrayList<>();
    
            three.addAll(one);
            for (int i = 0; i < two.size(); i++) {
                if (three.contains(two.get(i))){
                    three.remove(two.get(i));
                }else {
                    three.add(two.get(i));
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      在这种情况下你不应该使用 removeAll:

      listDir.removeAll(listDB);
      listDB.removeAll(listDir);
      

      因为一旦你从 listDir 中删除了公共元素“One”,listDB 仍然包含它并且不会被 listDB.removeAll(listDir) 删除,因为 listDir 不包含它。 因此,您最终会得到带有原始元素的 listDB。

      一种可能的解决方案是遍历列表并检查元素是否常见。 尽管列表大小相同,但您可以在同一个循环中遍历它们。

      for(int i=0;i<listDB.size();i++){
      
        if(!listDB.contains(listDir.get(i)){ 
          resultList.add(listDir.get(i))
        }
      
        if(!listDir.contains(listDB.get(i)){ 
          resultList.add(listDB.get(i))
        }
      
      }
      

      【讨论】:

        【解决方案3】:

        复制第一个列表并将其用于第二个列表中的removeAll。因为如果您从第一个列表中删除重复项,然后将其与第二个列表进行比较,则所有值都将是唯一的,因为重复项已从第一个列表中删除。

        Collection<String> listDir = new ArrayList(Arrays.asList("1","2", "3", "4", "5", "6", "7"));
        Collection<String> listDirCopy = new ArrayList<>();
        listDirCopy.addAll(listDir);
        Collection<String> listDB = new ArrayList(Arrays.asList("1","3", "5", "7", "9"));
        List<String> destinationList = new ArrayList<String>(); 
        
        listDir.removeAll(listDB);
        listDB.removeAll(listDirCopy);
        
        destinationList.addAll(listDir);
        destinationList.addAll(listDB);
        System.out.println(destinationList);
        

        【讨论】:

          猜你喜欢
          • 2015-12-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多