Java : Set with Examples

Set (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Set<E> methods.


Summary

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

Class diagram

final Set<String> set = new HashSet<>();

set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3

System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("X")); // false

Methods

boolean add (E e)

Adds the specified element to this set if it is not already present (optional operation).

final Set<String> set = new HashSet<>();
System.out.println(set); // []

System.out.println(set.add("a")); // true
System.out.println(set); // [a]

System.out.println(set.add("b")); // true
System.out.println(set); // [a, b]

System.out.println(set.add("c")); // true
System.out.println(set); // [a, b, c]

System.out.println(set.add("a")); // false
System.out.println(set); // [a, b, c]

boolean addAll (Collection<? extends E> c)

Adds all of the elements in the specified collection to this set if they're not already present (optional operation).

final var src = Set.of("a", "b", "c");

{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("d", "e", "f"))); // true
    System.out.println(set);  // [a, b, c, d, e, f]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("a", "b", "c"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.addAll(Set.of("a", "c", "e", "f"))); // true
    System.out.println(set); // [a, b, c, e, f]
}

void clear ()

Removes all of the elements from this set (optional operation).

final Set<String> set = new HashSet<>();
System.out.println(set); // []

set.add("a");
System.out.println(set); // [a]

set.add("b");
System.out.println(set); // [a, b]

set.clear();
System.out.println(set); // []

boolean contains (Object o)

Returns true if this set contains the specified element.

final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]

System.out.println(set.contains("")); // false
System.out.println(set.contains("a")); // true
System.out.println(set.contains("b")); // true
System.out.println(set.contains("c")); // true

System.out.println(set.contains("X")); // false
System.out.println(set.contains("abc")); // false

boolean containsAll (Collection<?> c)

Returns true if this set contains all of the elements of the specified collection.

final var set = Set.of("a", "b", "c");
System.out.println(set); // [c, b, a]

System.out.println(set.containsAll(Set.<String>of())); // true

System.out.println(set.containsAll(Set.of("a"))); // true
System.out.println(set.containsAll(Set.of("a", "b"))); // true
System.out.println(set.containsAll(Set.of("b", "a"))); // true
System.out.println(set.containsAll(Set.of("c", "b", "a"))); // true

System.out.println(set.containsAll(Set.of("X"))); // false
System.out.println(set.containsAll(Set.of("a", "X"))); // false
System.out.println(set.containsAll(Set.of("a", "b", "c", "X"))); // false

static <E> Set<E> copyOf (Collection<? extends E> coll)

Returns an unmodifiable Set containing the elements of the given Collection.

final Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]

final var copiedSet = Set.copyOf(set);
System.out.println(copiedSet); // [a, b, c]

set.add("d");
System.out.println(set); // [a, b, c, d]
System.out.println(copiedSet); // [a, b, c]

try {
    copiedSet.add("d");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

boolean equals (Object o)

Compares the specified object with this set for equality.

final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]

System.out.println(set.equals(Set.of("a"))); // false
System.out.println(set.equals(Set.of("a", "b"))); // false
System.out.println(set.equals(Set.of("a", "b", "c"))); // true
System.out.println(set.equals(Set.of("c", "b", "a"))); // true
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]

final Set<String> hashSet = new HashSet<>(set);
System.out.println(hashSet); // [a, b, c]
System.out.println(set.equals(hashSet)); // true

final Set<String> treeSet = new TreeSet<>(set);
System.out.println(treeSet); // [a, b, c]
System.out.println(set.equals(treeSet)); // true

final var list = new ArrayList<>(set);
System.out.println(list); // [a, b, c]
System.out.println(set.equals(list)); // false

int hashCode ()

Returns the hash code value for this set.

final var set1 = Set.of("a", "b", "c");
System.out.println(set1.hashCode()); // 294

final var set2 = Set.of(1, 2, 3);
System.out.println(set2.hashCode()); // 6
final Set<String> set1 = new LinkedHashSet<>();
set1.add("a");
set1.add("b");
set1.add("c");

System.out.println(set1); // [a, b, c]
System.out.println(set1.hashCode()); // 294

final Set<String> set2 = new LinkedHashSet<>();
set2.add("c");
set2.add("b");
set2.add("a");

System.out.println(set2); // [c, b, a]
System.out.println(set2.hashCode()); // 294

boolean isEmpty ()

Returns true if this set contains no elements.

final Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(set.isEmpty()); // false

set.clear();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true

Iterator<E> iterator ()

Returns an iterator over the elements in this set.

final var set = Set.of("aaa", "bbb", "ccc");
System.out.println(set); // [aaa, ccc, bbb]

final var it = set.iterator();

System.out.println("-- forEachRemaining --");
it.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//aaa
//ccc
//bbb

static <E> Set<E> of ()

Returns an unmodifiable set containing zero elements.

final var set = Set.of();
System.out.println(set); // []
System.out.println(set.size()); // 0

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1)

Returns an unmodifiable set containing one element.

final var set = Set.of("a");
System.out.println(set); // [a]
System.out.println(set.size()); // 1

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E... elements)

Returns an unmodifiable set containing an arbitrary number of elements.

final var set = Set.of(
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
        "k", "l", "m", "n"
);
System.out.println(set); // [c, d, e, f, g, h, i, j, k, l, m, n, a, b]
System.out.println(set.size()); // 14

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2)

Returns an unmodifiable set containing two elements.

final var set = Set.of("a", "b");
System.out.println(set); // [b, a]
System.out.println(set.size()); // 2

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2, E e3)

Returns an unmodifiable set containing three elements.

final var set = Set.of("a", "b", "c");
System.out.println(set); // [b, a, c]
System.out.println(set.size()); // 3

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2, E e3, E e4)

Returns an unmodifiable set containing four elements.

final var set = Set.of("a", "b", "c", "d");
System.out.println(set); // [b, a, d, c]
System.out.println(set.size()); // 4

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5)

Returns an unmodifiable set containing five elements.

final var set = Set.of("a", "b", "c", "d", "e");
System.out.println(set); // [e, d, c, b, a]
System.out.println(set.size()); // 5

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6)

Returns an unmodifiable set containing six elements.

final var set = Set.of("a", "b", "c", "d", "e", "f");
System.out.println(set); // [f, e, d, c, b, a]
System.out.println(set.size()); // 6

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7)

Returns an unmodifiable set containing seven elements.

final var set = Set.of("a", "b", "c", "d", "e", "f", "g");
System.out.println(set); // [g, f, e, d, c, b, a]
System.out.println(set.size()); // 7

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)

Returns an unmodifiable set containing eight elements.

final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h");
System.out.println(set); // [h, g, f, e, d, c, b, a]
System.out.println(set.size()); // 8

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)

Returns an unmodifiable set containing nine elements.

final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i");
System.out.println(set); // [a, i, h, g, f, e, d, c, b]
System.out.println(set.size()); // 9

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

static <E> Set<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)

Returns an unmodifiable set containing ten elements.

final var set = Set.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
System.out.println(set); // [j, i, h, g, f, e, d, c, b, a]
System.out.println(set.size()); // 10

try {
    set.add("z");
} catch (UnsupportedOperationException e) {
    System.out.println("UnsupportedOperationException!");
}

// Result
// ↓
//UnsupportedOperationException!

boolean remove (Object o)

Removes the specified element from this set if it is present (optional operation).

final var src = Set.of("a", "b", "c");

{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("")); // false
    System.out.println(set); // [a, b, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("a")); // true
    System.out.println(set); // [b, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("b")); // true
    System.out.println(set); // [a, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.remove("X")); // false
    System.out.println(set); // [a, b, c]
}

boolean removeAll (Collection<?> c)

Removes from this set all of its elements that are contained in the specified collection (optional operation).

final var src = Set.of("a", "b", "c");

{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.<String>of())); // false
    System.out.println(set); // [a, b, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a"))); // true
    System.out.println(set); // [b, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "b"))); // true
    System.out.println(set); // [c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("b", "a"))); // true
    System.out.println(set); // [c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "b", "c"))); // true
    System.out.println(set); // []
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("X", "Y"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.removeAll(Set.of("a", "X"))); // true
    System.out.println(set); // [b, c]
}

boolean retainAll (Collection<?> c)

Retains only the elements in this set that are contained in the specified collection (optional operation).

final var src = Set.of("a", "b", "c");

{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.<String>of())); // true
    System.out.println(set); // []
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("a"))); // true
    System.out.println(set); // [a]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("a", "b"))); // true
    System.out.println(set); // [a, b]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("b", "a"))); // true
    System.out.println(set); // [a, b]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("a", "b", "c"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("a", "b", "c", "X", "Y"))); // false
    System.out.println(set); // [a, b, c]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("X", "a", "b", "Y"))); // true
    System.out.println(set); // [a, b]
}
{
    final Set<String> set = new HashSet<>(src);
    System.out.println(set); // [a, b, c]

    System.out.println(set.retainAll(Set.of("X", "Y"))); // true
    System.out.println(set); // []
}

int size ()

Returns the number of elements in this set (its cardinality).

final var set1 = Set.of();
System.out.println(set1); // []
System.out.println(set1.size()); // 0

final var set2 = Set.of("a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3

default Spliterator<E> spliterator ()

Creates a Spliterator over the elements in this set.

final var set = Set.of("aaa", "bbb", "ccc");
System.out.println(set); // [bbb, ccc, aaa]

final var spliterator = set.spliterator();

System.out.println("-- forEachRemaining --");
spliterator.forEachRemaining(System.out::println);

// Result
// ↓
//-- forEachRemaining --
//bbb
//ccc
//aaa

Object[] toArray ()

Returns an array containing all of the elements in this set.

final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]

final Object[] array = set.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]

<T> T[] toArray (T[] a)

Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]

final String[] ret = set.toArray(new String[0]);
System.out.println(Arrays.toString(ret)); // [a, b, c]
final var set = Set.of("a", "b", "c");
System.out.println(set); // [a, b, c]

{
    final String[] array = new String[3];
    System.out.println(Arrays.toString(array)); // [null, null, null]

    final var ret = set.toArray(array);
    System.out.println(Arrays.toString(array)); // [a, b, c]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
    final String[] array = new String[5];
    System.out.println(Arrays.toString(array)); // [null, null, null, null, null]

    final var ret = set.toArray(array);
    System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
    System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
    final String[] array = new String[1];
    System.out.println(Arrays.toString(array)); // [null]

    final var ret = set.toArray(array);
    System.out.println(Arrays.toString(array)); // [null]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}

Methods declared in Collection

parallelStream, removeIf, stream, toArray

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.


Related posts

To top of page