Java : LinkedHashSet (順序を保持するセット) - API使用例
LinkedHashSet (Java SE 23 & JDK 23) の使い方まとめです。
ほとんどのメソッドにサンプルコードがあります。
APIドキュメントのおともにどうぞ。
概要
Setインタフェースのハッシュ表およびリンク・リスト実装。検出順序は明確に定義されています。 この実装は、HashSetとは異なります。
LinkedHashSet は、ハッシュ表とリンク・リストによる Set の実装です。
リンク・リストを持っているので、要素を追加した順序を保持します。
関連記事:Set(セット)の基本
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]
コンストラクタ
LinkedHashSet ()
デフォルトの初期容量(16)と負荷係数(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)
指定された初期容量とデフォルトの負荷係数(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)
指定された初期容量と負荷係数で新しい空のリンク・ハッシュ・セットを構築します。
負荷係数を変えて処理時間を計った例です。
(実行する環境によって変わる可能性があります)
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)
指定されたコレクションと同じ要素で新しいリンク・ハッシュ・セットを構築します。
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
メソッド
void addFirst (E e)
このコレクションの最初の要素として要素を追加します (オプションの操作)。
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)
このコレクションの最後の要素として要素を追加します (オプションの操作)。
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 ()
このコレクションの最初の要素を取得します。
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!");
}
// 結果
// ↓
//NoSuchElementException!
E getLast ()
このコレクションの最後の要素を取得します。
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!");
}
// 結果
// ↓
//NoSuchElementException!
static <T> LinkedHashSet<T> newLinkedHashSet (int numElements)
予想される要素数に適した新しい空のLinkedHashSetを作成します。
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 ()
このコレクションの最初の要素を削除して返します(オプションの操作)。
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!");
}
// 結果
// ↓
//NoSuchElementException!
E removeLast ()
このコレクションの最後の要素を削除して返します(オプションの操作)。
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!");
}
// 結果
// ↓
//NoSuchElementException!
SequencedSet<E> reversed ()
このコレクションの逆順viewを返します。
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 ()
このセット内の要素に対する遅延バインディングおよびフェイルファスト 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);
// 結果
// ↓
//aaa
//bbb
//ccc
HashSetで宣言されたメソッド
add, clear, clone, contains, isEmpty, iterator, newHashSet, remove, size, toArray, toArray
「Java API 使用例 : HashSet」をご参照ください。
AbstractSetで宣言されたメソッド
equals, hashCode, removeAll
「Java API 使用例 : AbstractSet」をご参照ください。
AbstractCollectionで宣言されたメソッド
addAll, containsAll, retainAll, toArray, toArray, toString
「Java API 使用例 : AbstractCollection」をご参照ください。
Collectionで宣言されたメソッド
parallelStream, removeIf, stream, toArray
「Java API 使用例 : Collection」をご参照ください。
Iterableで宣言されたメソッド
forEach
「Java API 使用例 : Iterable」をご参照ください。
Setで宣言されたメソッド
add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray
「Java API 使用例 : Set」をご参照ください。
関連記事
- List(リスト)の基本
- Map(マップ)の基本
- Set(セット)の基本
- List の初期化方法いろいろ
- Map の初期化方法いろいろ
- Set の初期化方法いろいろ
- 配列 vs. List
- 配列からListへの変換
- List から配列への変換
- API 使用例
- Collection (コレクション)
- Collections (コレクション操作)
- Comparable
- Comparator
- Iterator
- List (リスト)
- Map (マップ)
- Map.Entry (キーと値のペア)
- Queue (キュー)
- AbstractQueue
- ArrayBlockingQueue (ブロッキング・配列キュー)
- ArrayDeque (両端キュー)
- BlockingDeque (ブロッキング・両端キュー)
- BlockingQueue (ブロッキング・キュー)
- ConcurrentLinkedDeque (並列処理用・両端キュー)
- ConcurrentLinkedQueue (並列処理用キュー)
- Deque (両端キュー)
- LinkedBlockingDeque (ブロッキング・リンク両端キュー)
- LinkedBlockingQueue (ブロッキング・リンクキュー)
- LinkedList (二重リンク・リスト)
- PriorityBlockingQueue (ブロッキング・優先度付きキュー)
- PriorityQueue (優先度付きキュー)
- RandomAccess
- Set (セット)
- Spliterator