Collections Frameworkの概要
Collections Frameworkは、次の要素で構成されます。
- コレクション・インタフェース- コレクションを操作するプライマリの手段。
- Collection - オブジェクトのグループ。コレクションの順序付けおよび重複する要素を含んでいるかどうかについては何も考慮しない。
- Set - よく使用される、セットの抽象化。要素の重複は許可されない。順序付けはされていても、いなくてもよい。Collectionインタフェースを拡張する。
- List - 順序付けられたコレクションです。シーケンスとも呼ばれます。一般に重複は許可されます。位置によるアクセスを許可します。Collectionインタフェースを拡張する。
- Queue - 処理を行う前に要素を保持するために設計されたコレクション。基本的なCollectionオペレーションだけでなく、キューは追加の挿入、抽出、および検査オペレーションを提供する。
- Deque - 二重終端キュー。両端での要素の挿入と削除をサポートします。Queueインタフェースを拡張します。
- Map - キーから値へのマッピング。各キーを1つの値へマッピングできる。
- SortedSet - 自然順序 (Comparableインタフェースを参照)によって、またはSortedSetのインスタンスの生成時に提供されるComparatorオブジェクトによって、自動的にソートされる要素のセット。Setインタフェースを拡張する。
- SortedMap - マッピングがキーにより自動的にソートされるマップ。ソートは、キーの自然順序、またはSortedMapのインスタンスの生成時に提供されるComparatorを使用して行われます。Mapインタフェースを拡張します。
- NavigableSet - 指定の検索ターゲットにもっとも近い一致内容を報告するナビゲーション・メソッドで拡張されたSortedSet。昇順または降順の順序で、NavigableSetがアクセスしたり、トラバースしたりできる。
- NavigableMap - 指定の検索ターゲットにもっとも近い一致内容を返すナビゲーション・メソッドで拡張されたSortedMap。昇順または降順のキー順序で、NavigableMapがアクセスしたり、トラバースしたりできる。
- BlockingQueue - 要素の取得時にキューが空でなくなるまで待機したり、要素の格納時にキュー内に空きが生じるまで待機する操作を含むQueue。(このインタフェースは、java.util.concurrentパッケージの一部です)。
- TransferQueue - コンシューマが要素を受け取るまでプロデューサが待機するBlockingQueue。(このインタフェースは、java.util.concurrentパッケージの一部です)。
- BlockingDeque - 要素の取得時にデキューが空でなくなるまで待機したり、要素の格納時にデキュー内に空きが生じるまで待機する操作を含むDeque。DequeおよびBlockingQueueインタフェースを拡張します。(このインタフェースは、java.util.concurrentパッケージの一部です)。
- ConcurrentMap - 基本メソッド、putIfAbsent、removeおよびreplaceを使用するMap。(このインタフェースは、java.util.concurrentパッケージの一部です)。
- ConcurrentNavigableMap - NavigableMapでもあるConcurrentMap。
- 汎用の実装 - コレクション・インタフェースのプライマリ実装。
- HashSet - Setインタフェースのハッシュ表実装。Setインタフェースのもっとも用途の広い実装。
- TreeSet - NavigableSetインタフェースの赤 - 黒ツリー実装。
- LinkedHashSet - Setインタフェースのハッシュ表とリンク・リスト実装。HashSetと同等の実行速度を持つ、挿入順Set実装。
- ArrayList - Listインタフェースのサイズ変更可能な配列実装(同期をとらないVector)。Listインタフェースのもっとも用途の広い実装。
- ArrayDeque - Dequeインタフェースのサイズ変更が可能な効率的配列の実装。
- LinkedList - Listインタフェースの、ダブルリンク・リスト実装。要素がリスト内で頻繁に挿入あるいは削除される場合には、ArrayList実装よりも高いパフォーマンスを発揮する。また、Dequeインタフェースを実装する。Queueインタフェースを介してアクセスされた場合、LinkedListはFIFOキューとして動作する。
- PriorityQueue - アンバウンド形式の優先度キューのヒープ実装。
- HashMap - Mapインタフェースのハッシュ表実装(nullキーおよび値をサポートする、同期をとらないHashtable)。Mapインタフェースのもっとも用途の広い実装。
- TreeMap - NavigableMapインタフェースの赤 - 黒ツリー実装。
- LinkedHashMap - Mapインタフェースのハッシュ表とリンク・リスト実装。HashMapと同等の実行速度を持つ、挿入順Map実装。キャッシュを構築する場合にも役立つ(removeEldestEntry(Map.Entry)を参照)。
- ラッパー実装 - ほかの実装と併用して機能を拡張するための実装。staticファクトリ・メソッドを介して単独でアクセスされる。
- アダプタ実装 - コレクション・インタフェース間で適応させるための実装。
- 簡易実装 - コレクション・インタフェースの高性能な「ミニ実装」。
- レガシー実装 - 以前からあるコレクション・クラスが改良されて、コレクション・インタフェースを実装するようになった。
- Vector - 「レガシー・メソッド」を持つListインタフェースの、同期化され、サイズ変更可能な配列実装。
- Hashtable - nullのキーまたは値が許可されないMapインタフェースとレガシー・メソッドを持つ同期化されたハッシュ表実装。
- 特殊目的の実装
- WeakHashMap - キーへの弱参照だけを格納するMapインタフェースの実装。弱参照だけを格納することにより、キーがWeakHashMapの外部から参照されなくなると、キー値のペアをガベージ・コレクトすることができる。このクラスを利用すると、弱参照をもっとも簡単に利用することができる。キーがどのスレッドからもアクセスできなくなると、エントリの有用性がなくなるレジストリ的なデータ構造を実装する場合に役立つ。
- IdentityHashMap - ハッシュ表に基づく、識別情報ベースのMap実装。直列化やディープ・コピーなどのように、トポロジを維持しながらオブジェクト・グラフを変形する場合に、このクラスは便利です。このような変形を行うには、どのオブジェクトが表示済かを追跡する識別情報ベースの「ノード表」を保持する必要があります。識別情報ベースのマップを使用して、動的なデバッガや同様のシステムでオブジェクトとメタ情報のマッピングを保守することもできます。また、識別情報ベースのマップはequalsメソッドを意図的に歪ませることによって発生する「不正行為」を防ぐ場合に有用です。(IdentityHashMapがキーに対してequalsメソッドを呼び出すことはありません)。この実装のもう1つの利点は、高速だということです。
- CopyOnWriteArrayList - copy-on-write配列を基とするList実装。すべての変更オペレーション(add、set、removeなど)は、配列の新しいコピーの作成により実装される。反復中でも同期は不要であり、イテレータはConcurrentModificationExceptionをスローしないことが保証される。この実装はイベント・ハンドラ・リストの維持に最適(変更がまれで、トラバーサルは頻繁に発生し、時間がかかる可能性があるため)。
- CopyOnWriteArraySet - copy-on-write配列を基とするSet実装。この実装はCopyOnWriteArrayListと同様。ほとんどのSet実装と異なり、add、remove、およびcontainsメソッドでは、メソッドはセットのサイズに比例する時間が必要。この実装は、重複を回避する必要があるイベント・ハンドラ・リストの維持に最適。
- EnumSet - bit-vectorを基とする高性能のSet実装。各EnumSetインスタンスのすべての要素は、単一の列挙型の要素でなければならない。
- EnumMap - 配列を基とする高性能のMap実装。各EnumMapインスタンスのすべてのキーは、単一の列挙型の要素でなければならない。
- 並列実装 - これらの実装はjava.util.concurrentの一部である。
- 抽象実装 - カスタム実装を容易にする、コレクション・インタフェースの骨格実装。
- アルゴリズム - このCollectionsクラスには、次の便利なstaticメソッドが含まれている。
- sort(List) - マージ・ソート・アルゴリズムを使用してリストをソートします。このアルゴリズムは、高品質なクイックソートと同程度の性能を持ちます。クイックソートより優れている点として、O(n*log n)を保証するパフォーマンスと安定性があります。安定したソートとは、等価な要素の並べ替えを行わないソートのことです。
- binarySearch(List, Object) - バイナリ・サーチ・アルゴリズムを使ってソートされたリスト内の要素を検索する。
- reverse(List) - リスト内の要素の順序を逆転する。
- shuffle(List) - リスト内の要素をランダムに並べ替える。
- fill(List, Object) - リストのすべての要素を指定の値で上書きする。
- copy(List dest, List src) - 元のリストを宛先のリストにコピーする。
- min(Collection) - コレクション内の最小要素を返す。
- max(Collection) - コレクション内の最大要素を返す。
- rotate(List list, int distance) - リストにあるすべての要素を、指定した距離だけ回転させる。
- replaceAll(List list, Object oldVal, Object newVal) - 指定した値をすべて別の値に置き換える。
- indexOfSubList(List source, List target) - targetと等しいsourceの最初のサブリストのインデックスを返す。
- lastIndexOfSubList(List source, List target) - targetと等しいsourceの最後のサブリストのインデックスを返す。
- swap(List, int, int) - 指定したリスト内の指定した位置にある要素をスワップする。
- frequency(Collection, Object) - あるコネクションにおいて指定した要素が発生した回数を計算する。
- disjoint(Collection, Collection) - 2つのコレクションが互いに無関係である、つまり一般的な要素を含まないかどうかを判定する。
- addAll(Collection<? super T>, T...)- 指定された配列内のすべての要素を指定されたコレクションに追加する。
- インフラストラクチャ
- イテレータ - Enumerationインタフェースに似ているが、より強力で、メソッド名が改良されている。
- Iterator - Enumerationインタフェースの機能に加えて、たくみに定義された有用なセマンティックスによって、ユーザーが、基になるコレクションから要素を削除できるようになる。
- ListIterator - リストに対して使用するイテレータ。Iteratorインタフェースの機能に加えて、双方向の繰り返し、要素の置換、要素の挿入およびインデックスの取得をサポートする。
- 順序付け
- Comparable - これを実装するクラスに対し、自然順序付けの機能を提供する。自然順序付けは、リストのソート、またソートされたセットやマップ内の順序の維持に使用できる。多くのクラスが、このインタフェースを実装するために改良された。
- Comparator - 順序付けの関連を表し、リストのソート、またソートされたセットやマップ内の順序の維持に使用できる。ある型の自然順序付けをオーバーライドしたり、Comparableインタフェースを実装していない型のオブジェクトの順序付けを行うことができる。
- 実行時例外
- パフォーマンス
- RandomAccess - Listの実装が高速ランダム・アクセス(通常は一定時間)をサポートしていることを示すためのマーカー・インタフェース。これによりジェネリック・アルゴリズムがランダム・アクセス・リストまたは順次アクセス・リストのいずれかに適用されるとき、その動作を変更してパフォーマンスを向上することができる。
- 配列ユーティリティ
- Arrays - プリミティブ配列およびオブジェクト配列に対して、ソート、検索、比較、ハッシュ、コピー、サイズ変更、Stringへの変換、および埋込みを行うstaticメソッドを含む。