Java Language Sets Eliminating duplicates using Set

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Suppose you have a collection elements, and you want to create another collection containing the same elements but with all duplicates eliminated:

Collection<Type> noDuplicates = new HashSet<Type>(elements);

Example:

List<String> names = new ArrayList<>(
        Arrays.asList("John", "Marco", "Jenny", "Emily", "Jenny", "Emily", "John"));
Set<String> noDuplicates = new HashSet<>(names);
System.out.println("noDuplicates = " + noDuplicates);

Output:

noDuplicates = [Marco, Emily, John, Jenny]


Got any Java Language Question?