Skip to content
thesarfo

Reference

Set

Key properties of Java's Set interface — uniqueness, ordering guarantees per implementation, and the core Set interface methods.

views 0

Key points about Set

Unique elements — a Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction.

Ordering — elements in a Set can be sorted in ascending order using the TreeSet implementation. HashSet provides no ordering guarantees. LinkedHashSet maintains insertion order.

Null elements — all Set implementations permit, at most, one null element (if they permit null elements at all).

Methods — the Set interface includes all the methods of the Collection interface, and adds a stronger contract on the behavior of equals and hashCode, so Set instances can be compared meaningfully even if their implementation types differ.

Subinterfaces and implementations — the commonly used classes implementing Set are HashSet, LinkedHashSet, and TreeSet.

No position access — unlike List, Set doesn’t provide any methods to access an element by index, since it doesn’t maintain a fixed order.

Use cases — a Set is useful when you want to prevent duplicate values and don’t care about element order.

Equality — two Set objects are considered equal if they contain the same elements, regardless of order.

Thread safety — none of the Set implementations in the Java Collections Framework are thread-safe by default. You can make them thread-safe using Collections.synchronizedSet() or CopyOnWriteArraySet.

Set methods

  • boolean add(E e) — adds the specified element to this set if it is not already present (optional operation).
  • 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).
  • void clear() — removes all of the elements from this set (optional operation).
  • boolean contains(Object o) — returns true if this set contains the specified element.
  • boolean containsAll(Collection<?> c) — returns true if this set contains all of the elements of the specified collection.
  • boolean equals(Object o) — compares the specified object with this set for equality.
  • int hashCode() — returns the hash code value for this set.
  • boolean isEmpty() — returns true if this set contains no elements.
  • Iterator<E> iterator() — returns an iterator over the elements in this set.
  • boolean remove(Object o) — removes the specified element from this set if it is present (optional operation).
  • boolean removeAll(Collection<?> c) — removes from this set all of its elements that are contained in the specified collection (optional operation).
  • boolean retainAll(Collection<?> c) — retains only the elements in this set that are contained in the specified collection (optional operation).
  • int size() — returns the number of elements in this set (its cardinality).
  • default Spliterator<E> spliterator() — creates a Spliterator over the elements in this set.