Java : LinkedHashSet with Examples

LinkedHashSet (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the LinkedHashSet<E> methods.


Summary

Hash table and linked list implementation of the Set interface, with well-defined encounter 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.066358 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.104886 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.152455 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.102799 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

void addFirst (E e)

Adds an element as the first element of this collection (optional operation).

final var set = new LinkedHashSet<String>();
System.out.println(set); // []

set.addFirst("aaa");
System.out.println(set); // [aaa]

set.addFirst("bbb");
System.out.println(set); // [bbb, aaa]

set.addFirst("ccc");
System.out.println(set); // [ccc, bbb, aaa]

void addLast (E e)

Adds an element as the last element of this collection (optional operation).

final var set = new LinkedHashSet<String>();
System.out.println(set); // []

set.addLast("aaa");
System.out.println(set); // [aaa]

set.addLast("bbb");
System.out.println(set); // [aaa, bbb]

set.addLast("ccc");
System.out.println(set); // [aaa, bbb, ccc]

E getFirst ()

Gets the first element of this collection.

final var set = new LinkedHashSet<String>();
System.out.println(set); // []

set.addLast("b");

System.out.println(set); // [b]
System.out.println(set.getFirst()); // b

set.addLast("c");

System.out.println(set); // [b, c]
System.out.println(set.getFirst()); // b

set.addFirst("a");

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

try {
    set.getFirst();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

E getLast ()

Gets the last element of this collection.

final var set = new LinkedHashSet<String>();
System.out.println(set); // []

set.addLast("b");

System.out.println(set); // [b]
System.out.println(set.getLast()); // b

set.addFirst("a");

System.out.println(set); // [a, b]
System.out.println(set.getLast()); // b

set.addLast("c");

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

try {
    set.getLast();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

static <T> LinkedHashSet<T> newLinkedHashSet (int numElements)

Creates a new, empty LinkedHashSet suitable for the expected number of elements.

final var set = LinkedHashSet.<String>newLinkedHashSet(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

E removeFirst ()

Removes and returns the first element of this collection (optional operation).

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

set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

System.out.println(set.removeFirst()); // aaa
System.out.println(set); // [bbb, ccc]

System.out.println(set.removeFirst()); // bbb
System.out.println(set); // [ccc]

System.out.println(set.removeFirst()); // ccc
System.out.println(set); // []
final var set = new LinkedHashSet<String>();
System.out.println(set); // []

try {
    var _ = set.removeFirst();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

E removeLast ()

Removes and returns the last element of this collection (optional operation).

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

set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

System.out.println(set.removeLast()); // ccc
System.out.println(set); // [aaa, bbb]

System.out.println(set.removeLast()); // bbb
System.out.println(set); // [aaa]

System.out.println(set.removeLast()); // aaa
System.out.println(set); // []
final var set = new LinkedHashSet<String>();
System.out.println(set); // []

try {
    var _ = set.removeLast();
} catch (NoSuchElementException e) {
    System.out.println("NoSuchElementException!");
}

// Result
// ↓
//NoSuchElementException!

SequencedSet<E> reversed ()

Returns a reverse-ordered view of this collection.

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

set.add("aaa");
set.add("bbb");
set.add("ccc");

System.out.println(set); // [aaa, bbb, ccc]

final var reversedSet = set.reversed();
System.out.println(reversedSet); // [ccc, bbb, aaa]

System.out.println(reversedSet.reversed()); // [aaa, bbb, ccc]

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, newHashSet, 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