HashSet
A set holds unique values. add returns false for duplicates.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<Integer> s = new TreeSet<>(List.of(3, 1, 2));
System.out.println(s.add(2) + " " + s.add(9) + " " + s);
}
}Result
false true [1, 2, 3, 9]
Exercise
Unique collection.
Set<Integer> s = new <>();
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.