Java : HashSet with Examples
HashSet (Java SE 18 & JDK 18) API Examples.
You will find code examples on most HashSet methods.
Summary
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 ()
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)
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.125983 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.170036 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
HashSet (int initialCapacity, float loadFactor)
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.260103 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.173419 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
HashSet (Collection<? extends E> c)
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)
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 ()
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 ()
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)
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 ()
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 ()
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
boolean remove (Object o)
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 ()
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 ()
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 ()
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)
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