简体   繁体   English

从两个数组列表中获取唯一值

[英]Getting unique values from two arraylists with Collections

Looked several answers on stack, tried to do it with help of this one Simple way to compare 2 ArrayLists but can't try to figure out what seems to be a problem. 在堆栈上看了几个答案,试图借助这种比较2个ArrayList的简单方法来做到这一点,但是却无法找出问题所在。 To summarize the code that isnt visible, I've created two arraylists that contain 4 files names. 为了总结不可见的代码,我创建了两个包含4个文件名的arraylist。 Now im trying to get the third arraylist which will contain only unique values from these two arraylists. 现在,我试图获取第三个arraylist,它将仅包含这两个arraylist中的唯一值。 Example: 1st arraylist - One, Two, Three, Four 2nd arraylist - One, Three, Five, Seven 3rd arraylist - Two, Four, Five, Seven (solution arraylist) Here is the code: 示例:第一数组列表-一,二,三,四个第二数组列表-一,三,五,七第三数组列表-二,四个,五,七(解决方案数组列表)这是代码:

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);

You shouldn't use removeAll in this case: 在这种情况下,您不应使用removeAll:

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

Because once you remove the common element 'One' from listDir, the listDB still contains it and won't be removed by listDB.removeAll(listDir) because listDir doesn't contains it. 因为一旦从listDir中删除公共元素“ One”,listDB仍包含它,并且listDB.removeAll(listDir)不会将其删除,因为listDir不包含它。 So you end up with listDB with it's original elements. 因此,您最终获得了listDB及其原始元素。

One possible solution would be to travers both list and check if an element is common. 一种可能的解决方案是遍历列表并检查元素是否公用。 Despite the lists are the same size you can travers them in the same loop. 尽管列表大小相同,但是您可以在同一循环中遍历它们。

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))
  }

}

Make a duplicate of the first list and use it to removeAll from second list. 做第一个列表的副本,并用它removeAll从第二个列表。 Because if you remove duplicates from first list and then compare it with second list all the values will be unique as the duplicates were already removed from first list. 因为如果您从第一个列表中删除重复项,然后将其与第二个列表进行比较,则所有值将是唯一的,因为重复项已从第一个列表中删除。

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);

Hello sorry for my beginner code here but can you maybe make the third arraylist, loop through the first and then add all the elements in the first array list. 您好,对不起我的初学者代码,但是您可以制作第三个arraylist,循环遍历第一个,然后在第一个数组列表中添加所有元素。 Then loop through the second list and add the elements in the 3rd array list if it does not exist or remove if it exists. 然后循环遍历第二个列表,并在第三个数组列表中添加元素(如果不存在)或删除(如果存在)。 Look at the following code, hope it helps 看下面的代码,希望对您有所帮助

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));
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM