Java : LinkedHashSet with Examples

LinkedHashSet (Java SE 18 & JDK 18) API Examples.
You will find code examples on most LinkedHashSet methods.


Summary

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries.

Class diagram

final var set = new LinkedHashSet<String>();

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
final var linkedHashSet = new LinkedHashSet<String>();
linkedHashSet.add("xxx");
linkedHashSet.add("yyy");
linkedHashSet.add("aaa");
linkedHashSet.add("bbb");

System.out.println(linkedHashSet); // [xxx, yyy, aaa, bbb]

final var hashSet = new HashSet<String>();
hashSet.add("xxx");
hashSet.add("yyy");
hashSet.add("aaa");
hashSet.add("bbb");

System.out.println(hashSet); // [aaa, bbb, yyy, xxx]

Constructors

LinkedHashSet ()

Constructs a new, empty linked hash set with the default initial capacity (16) and load factor (0.75).

final var set = new LinkedHashSet<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

LinkedHashSet (int initialCapacity)

Constructs a new, empty linked hash set with the specified initial capacity and the default load factor (0.75).

final var set = new LinkedHashSet<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.164871 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var set = new LinkedHashSet<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.203662 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);

LinkedHashSet (int initialCapacity, float loadFactor)

Constructs a new, empty linked hash set with the specified initial capacity and load factor.

final var set = new LinkedHashSet<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.302754 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);
final var set = new LinkedHashSet<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.220326 sec.
System.out.printf("%f sec.%n", (endTime - startTime) / 1000000000.0);

LinkedHashSet (Collection<? extends E> c)

Constructs a new linked hash set with the same elements as the specified collection.

final var c = List.of("a", "b", "c");

final var set = new LinkedHashSet<>(c);
System.out.println(set); // [a, b, c]
System.out.println(set.size()); // 3

Methods

Spliterator<E> spliterator ()

Creates a late-binding and fail-fast Spliterator over the elements in this set.

final var set = new LinkedHashSet<String>();
Collections.addAll(set, "aaa", "bbb", "ccc");
System.out.println(set); // [aaa, bbb, ccc]

final var spliterator = set.spliterator();
spliterator.forEachRemaining(System.out::println);

// Result
// ↓
//aaa
//bbb
//ccc

Methods declared in HashSet

add, clear, clone, contains, isEmpty, iterator, remove, size, toArray, toArray

Please see the link below.

Methods declared in AbstractSet

equals, hashCode, removeAll

Please see the link below.

Methods declared in AbstractCollection

addAll, containsAll, retainAll, toArray, toArray, toString

Please see the link below.

Methods declared in Collection

parallelStream, removeIf, stream, toArray

Please see the link below.

Methods declared in Iterable

forEach

Please see the link below.

Methods declared in Set

add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray

Please see the link below.


Related posts

To top of page