1

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

3 Answers 3

1

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

}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

This code does not seem to work. The printed results are [2, 4, 6, 9].
@stoneMaster that is the expected result for unique values from both lists. 2,4,6 from the first list and 9 from the second list. Are you trying to solve a different problem?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.