Created: 2023-09-03 20:50
Status: #concept
Subject: Programming
Tags: Java java.util Java Interface java.util.Collection
java.util.Set
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 concept.
All Known Implementing Classes:
Set<String> set = new HashSet<>();
set.add("one");
set.add("one");
set.add("two");
for (String element: set) {
System.out.println(element);
}
// one
// two
- If objects created from custom classes are added to the
HashSet
object, they must have both theequals
andhashCode
methods defined.