Java : LinkedHashSet with Examples
LinkedHashSet (Java SE 18 & JDK 18) API Examples.
You will find code examples on most LinkedHashSet methods.
Summary
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 ()
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)
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)
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)
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 ()
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
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
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
Please see the link below.
Related posts
- API Examples