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
- Deduplicate a list by adding elements to a
HashSet
. - Show that sets don’t preserve order; contrast with
LinkedHashSet
.