简体   繁体   中英

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. To summarize the code that isnt visible, I've created two arraylists that contain 4 files names. Now im trying to get the third arraylist which will contain only unique values from these two arraylists. 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:

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. So you end up with listDB with it's original elements.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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