🐡

JavaのCollection Interface

2021/04/27に公開

Collection interfaceというのがある。このインターフェイスに基づいて、いろんな種類のコレクションのサブインターフェースおよびサブタイプが実装されている。

Collection interfaceから例えば、以下のサブインターフェイスが実装されている。

  • List Interface
  • Set Interface

List Interfaceを実装するクラスとしては、

  • ArrayList
  • LinkedList
  • ImmutableCollections$ListN

などがある。また、Set Interfaceを実装するクラスとしては以下がある。

  • ImmutableCollections$SetN
  • HashSet

Set Interfaceをさらに拡張したインターフェイスというのもあって SortedSet Interfaceがそれにあたる。
SortedSetInterfaceを実装するクラスとしては、TreeSetクラスがある。

以下のようにまとめることができる。

Collection<Integer> col1 = List.of(0,65,1);
Collection<Integer> col2 = Set.of(0,65,1);
System.out.println(col1.equals(col2)); // False

List.ofメソッドは、static factory methodと呼ばれるもので、変更不可能なcollectionを作成する。Set.ofメソッドも同様である。

参考

Discussion