Skip to content

Sets: unique values

Watch it run · 1 of 3

Adding a duplicate

1import java.util.*;
2
3public class Main {
4 public static void main(String[] args) {
5 Set<Integer> seen = new HashSet<>();
6 seen.add(5);
7 seen.add(1);
8 seen.add(5);
9 System.out.println(seen.size() + " " + seen.contains(1) + " " + seen.contains(7));
10 }
11}

Memory

Set seen

empty

Output

(nothing yet)

line 5An empty set.

step 1 of 5