Remove all objects in an arraylist that exist in another arraylist

JavaArraylist

Java Problem Overview


I'm trying to read in from two files and store them in two separate arraylists. The files consist of words which are either alone on a line or multiple words separated by commas. I read each file with the following code (not complete):

ArrayList<String> temp = new ArrayList<>();

FileInputStream fis;
fis = new FileInputStream(fileName);

Scanner scan = new Scanner(fis);

while (scan.hasNextLine()) {
    Scanner input = new Scanner(scan.nextLine());
    input.useDelimiter(",");
    while (scan.hasNext()) {
        String md5 = scan.next();
        temp.add(md5);
    }
}
scan.close();    

return temp;

I now need to read two files in and remove all words from the first file which also exist in the second file (there are some duplicate words in the files). I have tried with for-loops and other such stuff, but nothing has worked so any help would be greatly appreciated!

Bonus question: I also need to find out how many duplicates there are in the two files - I've done this by adding both arraylists to a HashSet and then subtracting the size of the set from the combined size of the two arraylists - is this a good solution, or could it be done better?

Java Solutions


Solution 1 - Java

You can use the removeAll method to remove the items of one list from another list.

To obtain the duplicates you can use the retainAll method, though your approach with the set is also good (and probably more efficient)

Solution 2 - Java

The collection facility has a convenient method for this purpose:

list1.removeAll(list2);

Solution 3 - Java

First you need to override equal method in your custom class and define the matching criteria of removing list

public class CustomClass{

 @Override
    public boolean equals(Object obj) {

        try {
            CustomClass licenceDetail  = (CustomClass) obj;
            return name.equals(licenceDetail.getName());
        }
        catch (Exception e)
        {
            return false;
        }

    }
}

Second you call the removeAll() method

> list1.removeAll(list2);

Solution 4 - Java

As others have mentioned, use the Collection.removeAll method if you wish to remove all elements that exist in one Collection from the Collection you are invoking removeall on.

As for your bonus question, I'm a huge fan of Guava's Sets class. I would suggest the use of Sets.intersection as follows:

Sets.intersection(wordSetFromFile1, wordSetFromFile2).size();

Assuming you created a Set of words from both files, you can determine how many distinct words they have in common with that one liner.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeorgeWChubbyView Question on Stackoverflow
Solution 1 - JavaJoniView Answer on Stackoverflow
Solution 2 - JavaMordechaiView Answer on Stackoverflow
Solution 3 - JavaShahab RaufView Answer on Stackoverflow
Solution 4 - JavawhaleyView Answer on Stackoverflow