Se

Sets in Java

1 exercise

About Sets

A Set is an unordered collection that (unlike List) is guaranteed to not contain any duplicate values.

The generic type parameter of the Set interface denotes the type of the elements contained in the Set:

Set<Integer> ints = Set.of(1, 2, 3);
Set<String> strings = Set.of("alpha", "beta", "gamma");
Set<Object> mixed = Set.of(1, false, "foo");

Note that the Set.of() method creates an unmodifiable Set instance. Trying to call methods like add and remove on this instance will result in an exception at run-time.

To create a modifiable Set, you need to instantiate a class that implements the Set interface. The most-used built-in class that implements this interface is the HashSet class.

Set<Integer> ints = new HashSet<>();

The Set interface extends from the Collection and Iterable interfaces, and therefore shares a lot of methods with other types of collections. A notable difference to the Collection interface, however, is that methods like add and remove return a boolean (instead of void) which indicates whether the item was contained in the set when that method was called:

Set<Integer> set = new HashSet<>();
set.add(1);
// => true
set.add(2);
// => true
set.add(1);
// => false
set.size();
// => 2
set.contains(1);
// => true
set.contains(3);
// => false
set.remove(3);
// => false
set.remove(2);
// => true
set.size();
// => 1
Edit via GitHub The link opens in a new window or tab

Learn Sets