Java : HashSet with Examples
HashSet (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the HashSet<E> methods.
Summary
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.
final var set = new HashSet<String>();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
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
Constructors
HashSet ()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load factor (0.75).
final var set = new HashSet<String>();
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
HashSet (int initialCapacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).
final var set = new HashSet<String>(10000000);
System.out.println(set); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
set.add(String.valueOf(i));
}
final var endTime = System.nanoTime();
// 0.051620 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var set = new HashSet<String>(1);
System.out.println(set); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
set.add(String.valueOf(i));
}
final var endTime = System.nanoTime();
// 0.083717 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
HashSet (int initialCapacity, float loadFactor)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and the specified load factor.
final var set = new HashSet<String>(16, 0.05f);
System.out.println(set); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
set.add(String.valueOf(i));
}
final var endTime = System.nanoTime();
// 0.130706 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var set = new HashSet<String>(16, 0.75f);
System.out.println(set); // []
final var startTime = System.nanoTime();
for (int i = 0; i < 1000000; i++) {
set.add(String.valueOf(i));
}
final var endTime = System.nanoTime();
// 0.101407 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
HashSet (Collection<? extends E> c)
Constructs a new set containing the elements in the specified collection.
final var c = List.of("a", "b", "c");
final var set = new HashSet<>(c);
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
Methods
boolean add (E e)
Adds the specified element to this set if it is not already present.
final var set = new HashSet<String>();
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]
void clear ()
Removes all of the elements from this set.
final var set = new HashSet<String>();
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); // []
Object clone ()
Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.
final var set = new HashSet<String>();
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
final var cloned = set.clone();
System.out.println(cloned); // [a, b, c]
System.out.println(cloned.getClass()); // class java.util.HashSet
boolean contains (Object o)
Returns true if this set contains the specified element.
final var set = new HashSet<String>();
Collections.addAll(set, "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 isEmpty ()
Returns true if this set contains no elements.
final var set = new HashSet<String>();
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 = new HashSet<String>();
Collections.addAll(set, "aaa", "bbb", "ccc");
System.out.println(set); // [aaa, ccc, bbb]
set.iterator().forEachRemaining(System.out::println);
// Result
// ↓
//aaa
//ccc
//bbb
static <T> HashSet<T> newHashSet (int numElements)
Creates a new, empty HashSet suitable for the expected number of elements.
final var set = HashSet.<String>newHashSet(3);
System.out.println(set); // []
System.out.println(set.isEmpty()); // true
set.add("a");
set.add("b");
set.add("c");
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3
boolean remove (Object o)
Removes the specified element from this set if it is present.
final var src = Set.of("a", "b", "c");
{
final var 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 var 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 var 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 var 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]
}
int size ()
Returns the number of elements in this set (its cardinality).
final var set1 = new HashSet<String>();
System.out.println(set1); // []
System.out.println(set1.size()); // 0
final var set2 = new HashSet<String>();
Collections.addAll(set2, "a", "b", "c");
System.out.println(set2); // [a, b, c]
System.out.println(set2.size()); // 3
Spliterator<E> spliterator ()
Creates a late-binding and fail-fast Spliterator over the elements in this set.
final var set = new HashSet<String>();
Collections.addAll(set, "aaa", "bbb", "ccc");
System.out.println(set); // [aaa, ccc, bbb]
final var spliterator = set.spliterator();
spliterator.forEachRemaining(System.out::println);
// Result
// ↓
//aaa
//ccc
//bbb
Object[] toArray ()
Returns an array containing all of the elements in this collection.
final var set = new HashSet<String>();
Collections.addAll(set, "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 collection; the runtime type of the returned array is that of the specified array.
final var set = new HashSet<String>();
Collections.addAll(set, "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 = new HashSet<String>();
Collections.addAll(set, "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 AbstractSet
Methods declared in AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, toString
Please see the link below.
Methods declared in Collection
Methods declared in Iterable
Methods declared in Set
addAll, containsAll, equals, hashCode, removeAll, retainAll
Please see the link below.
Related posts
- API Examples