Java - HashSet

HashSet

import java.util.*;
Set uniq = new HashSet<>(List.of(1,2,2,3)); // {1,2,3}
System.out.println(uniq.contains(2));

Set enforces uniqueness; order is not guaranteed with HashSet.

Try it

  1. Deduplicate a list by adding elements to a HashSet.
  2. Show that sets don’t preserve order; contrast with LinkedHashSet.