Site icon Automation Dojos

Remove Duplicates from List Using Set.addAll

The Set interface has a method called addAll. This accepts a ‘Collection’ as the parameter. So if you invoke this method by passing the ‘List’, the duplicates will be eliminated. The following code snippet demonstrates this method.

Here, a new LinkedHashSet is created and the addAll method is invoked by passing the List as input. So this will automatically create a Set with the elements in the List and eliminate the duplicates.

Hence this code prints the following output:

5 10 15 20 35 40 25
Exit mobile version