Java - Set (Collection)

1 - About

This interface is a member of the Java Collections Framework.

Set is a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine.

See java.util.Set

Using a HashSet, contains is a constant-time operation. This is much better than a linear search through an array.

3 - Restrictions / Prerequisites

  • <note important>The Set collection doesn't allow duplicate and use the equals and hashCode methods to ensure this integrity. This methods must be then implemented in the element of your collection.</note>
  • It is not permissible for a set to contain itself as an element.
  • The behaviour of a set is not specified with mutable objects as element. (If the value of an object is changed in a manner that affects equals comparisons).

4 - Methods

4.1 - Basic Methods

int size();
boolean isEmpty();
boolean contains(Object element);
boolean add(E element);  // optional
boolean remove(Object element); // optional
Iterator<E> iterator();

4.2 - Bulk Methods

boolean containsAll(Collection<?> c);
boolean addAll(Collection<? extends E> c); // optional
boolean removeAll(Collection<?> c);  // optional
boolean retainAll(Collection<?> c); // optional
void clear(); // optional

4.3 - Array Methods

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

5 - Management

5.1 - Creation

The Java platform contains three general-purpose Set implementations:

It stores its elements in a hash table, is the best-performing implementation; however it makes no guarantees concerning the order of iteration.

Set<String> myHashSetMap= new HashSet<String>();
// or with a initial set
Set<String> set = new HashSet<>(Arrays.asList("first element"));
// or
Stream.of("a", "b").collect(Collectors.toSet());

  • TreeSet,

It stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet.

  • LinkedHashSet.

It is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set (insertion-order). LinkedHashSet spares its clients from the unspecified, generally chaotic ordering provided by HashSet at a cost that is only slightly higher.

  • SortedSet

a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls.

5.2 - Remove a key


5.3 - To list

Java - List (sequence)

  • from list instantiation

List<T> ts = new ArrayList<T>(mySet);
List<T> ts = new ArrayList<T>();

List<String> list =;

6 - How to

6.1 - Get no Duplicate in a set

As a Set which, by definition, cannot contain a duplicate, the function works by using the standard conversion constructor described in the The Collection Interface section.

The equals and hashCode methods must be implemented.

If you have a Collection c where you want to eliminate all duplicates.

  • In on line

Collection<Type> noDups = new HashSet<Type>(c);
// of to preserve the order of the original collection
Collection<Type> noDups = new LinkedHashSet<Type>(c);

  • With a method:

public static <E> Set<E> removeDups(Collection<E> c) {
    return new LinkedHashSet<E>(c);

7 - Documentation / Reference

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap