Java Set

The collection interface for a group of unique elements, where adding a duplicate has no effect.

In more detail

HashSet has no predictable order; LinkedHashSet keeps insertion order. Uniqueness relies on equals and hashCode, so set elements must implement them correctly. Use a set when the contract is membership rather than position.

Example

Set<String> set = new LinkedHashSet<>(list);  // size 1 for ["a", "a"]

Related terms

Taught in

References

Certification courses