広告

Java : List (リスト) - API使用例

List (Java SE 20 & JDK 20) の使用例まとめです。
だいたいのメソッドを網羅済みです。
API仕様のおともにどうぞ。


概要

順序付けられたコレクションです。シーケンスとも呼ばれます。 このインタフェースのユーザーは、リスト内のどこに各要素が挿入されるかを精密に制御できます。 ユーザーは整数値のインデックス(リスト内の位置)によって要素にアクセスしたり、リスト内の要素を検索したりできます。

クラス構成

List は順序付けされたコレクションです。
要素を追加するたびに 0, 1, 2, 3 ... とインデックスが割り振られて、厳密に順序が管理されます。

配列と似ていますが、配列と違いサイズを気にしなくてよいのが List の大きなメリットです。

final List<String> list = new ArrayList<>();
System.out.println(list); // []

list.add("aaa");
list.add("bbb");
list.add("ccc");
System.out.println(list); // [aaa, bbb, ccc]

System.out.println(list.get(0)); // aaa
System.out.println(list.get(1)); // bbb
System.out.println(list.get(2)); // ccc

List の基本は下記の記事にもまとめています。
そちらも参考にしていただければ幸いです。


メソッド

void add (int index, E element)

このリスト内の指定された位置に、指定された要素を挿入します(オプションの操作)。

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(0, "A");
    System.out.println(list); // [A, a, b, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(1, "B");
    System.out.println(list); // [a, B, b, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(2, "C");
    System.out.println(list); // [a, b, C, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(3, "D");
    System.out.println(list); // [a, b, c, D]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    //list.add(4, "D"); // IndexOutOfBoundsException
}

boolean add (E e)

指定された要素をこのリストの最後に追加します(オプションの操作)。

final List<String> list = new ArrayList<>();
System.out.println(list); // []

System.out.println(list.add("a")); // true
System.out.println(list); // [a]

System.out.println(list.add("b")); // true
System.out.println(list); // [a, b]

System.out.println(list.add("c")); // true
System.out.println(list); // [a, b, c]

boolean addAll (int index, Collection<? extends E> c)

指定されたコレクション内のすべての要素を、このリストの指定された位置に挿入します(オプションの操作)。

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(0, List.of("A1", "A2"))); // true
    System.out.println(list); // [A1, A2, a, b, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(1, List.of("B1", "B2", "B3"))); // true
    System.out.println(list); // [a, B1, B2, B3, b, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(2, List.of("C1", "C2", "C3", "C4"))); // true
    System.out.println(list); // [a, b, C1, C2, C3, C4, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(3, List.of())); // false
    System.out.println(list); // [a, b, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    //list.addAll(4, List.of("E")); // IndexOutOfBoundsException
}
final var src = List.of("a", "b", "c");
System.out.println(src);// [a, b, c]

final List<String> list = new ArrayList<>(src);

// Setは順序が保証されません。
System.out.println(list.addAll(3, Set.of("D1", "D2", "D3"))); // true
System.out.println(list); // [a, b, c, D3, D2, D1]

boolean addAll (Collection<? extends E> c)

指定されたコレクション内のすべての要素を、指定されたコレクションのイテレータによって返される順序で、このリストの最後に追加します(オプションの操作)。

final List<String> list = new ArrayList<>();

System.out.println(list.addAll(List.of("a1"))); // true
System.out.println(list); // [a1]

System.out.println(list.addAll(List.of("b1", "b2"))); // true
System.out.println(list); // [a1, b1, b2]

System.out.println(list.addAll(List.of("c1", "c2", "c3"))); // true
System.out.println(list); // [a1, b1, b2, c1, c2, c3]

System.out.println(list.addAll(List.of())); // false
System.out.println(list); // [a1, b1, b2, c1, c2, c3]
final List<String> list = new ArrayList<>();
System.out.println(list); // []

// Setは順序が保証されません。
System.out.println(list.addAll(Set.of("c1", "c2", "c3"))); // true
System.out.println(list); // [c3, c2, c1]

void clear ()

すべての要素をこのリストから削除します(オプションの操作)。

final List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");

System.out.println(list); // [a, b, c]

list.clear();
System.out.println(list); // []

boolean contains (Object o)

指定された要素がこのリストに含まれている場合にtrueを返します。

final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

System.out.println(list.contains("")); // false
System.out.println(list.contains("a")); // true
System.out.println(list.contains("b")); // true
System.out.println(list.contains("c")); // true

System.out.println(list.contains("abc")); // false

boolean containsAll (Collection<?> c)

指定されたコレクションのすべての要素がこのリストに含まれている場合にtrueを返します。

final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

System.out.println(list.containsAll(List.of())); // true

System.out.println(list.containsAll(List.of("a"))); // true
System.out.println(list.containsAll(List.of("a", "b"))); // true
System.out.println(list.containsAll(List.of("b", "a"))); // true
System.out.println(list.containsAll(List.of("c", "b", "a"))); // true

System.out.println(list.containsAll(List.of("d"))); // false
System.out.println(list.containsAll(List.of("a", "d"))); // false
System.out.println(list.containsAll(List.of("a", "b", "c", "d"))); // false

static <E> List<E> copyOf (Collection<? extends E> coll)

指定されたCollectionの要素をその反復順序で含む「変更不可能なリスト」を返します。

final List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");

System.out.println(list); // [a, b, c]

final var copiedList = List.copyOf(list);
System.out.println(copiedList); // [a, b, c]

list.clear();
System.out.println(list); // []
System.out.println(copiedList); // [a, b, c]

//copiedList.add("d"); // UnsupportedOperationException
//copiedList.clear(); // UnsupportedOperationException

boolean equals (Object o)

指定されたオブジェクトがこのリストと等しいかどうかを比較します。

final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

System.out.println(list.equals(List.of("a"))); // false
System.out.println(list.equals(List.of("a", "b"))); // false
System.out.println(list.equals(List.of("a", "b", "c"))); // true
System.out.println(list.equals(List.of("c", "b", "a"))); // false
final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

final var arrayList = new ArrayList<>(list);
System.out.println(arrayList); // [a, b, c]
System.out.println(list.equals(arrayList)); // true

final var linkedList = new LinkedList<>(list);
System.out.println(linkedList); // [a, b, c]
System.out.println(list.equals(linkedList)); // true
final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

// List以外はfalse
final var set = new LinkedHashSet<>();
set.add("a");
set.add("b");
set.add("c");

System.out.println(set); // [a, b, c]
System.out.println(list.equals(set)); // false

E get (int index)

このリスト内の指定された位置にある要素を返します。

final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

//list.get(-1); // IndexOutOfBoundsException
System.out.println(list.get(0)); // a
System.out.println(list.get(1)); // b
System.out.println(list.get(2)); // c
//list.get(3); // IndexOutOfBoundsException
final var list = List.of(10, 20, 30, 40);
System.out.println(list); // [10, 20, 30, 40]

System.out.println(list.get(0)); // 10
System.out.println(list.get(1)); // 20
System.out.println(list.get(2)); // 30
System.out.println(list.get(3)); // 40

int hashCode ()

このリストのハッシュ・コード値を返します。

final var list1 = List.of("a", "b", "c");
System.out.println(list1.hashCode()); // 126145

final var list2 = List.of("A", "B", "C");
System.out.println(list2.hashCode()); // 94369

final var list3 = List.of(1, 2, 3);
System.out.println(list3.hashCode()); // 30817

int indexOf (Object o)

指定された要素がこのリスト内で最初に検出された位置のインデックスを返します。指定された要素がこのリストにない場合は -1を返します。

final var list = List.of("a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]

System.out.println(list.indexOf("")); // -1
System.out.println(list.indexOf("a")); // 0
System.out.println(list.indexOf("b")); // 1
System.out.println(list.indexOf("c")); // 2
System.out.println(list.indexOf("d")); // -1
System.out.println(list.indexOf("abc")); // -1

boolean isEmpty ()

このリストに要素がない場合にtrueを返します。

final List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");

System.out.println(list); // [a, b, c]
System.out.println(list.isEmpty()); // false

list.clear();
System.out.println(list); // []
System.out.println(list.isEmpty()); // true

Iterator<E> iterator ()

このリスト内の要素を適切な順序で反復するイテレータを返します。

final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

final var iterator = list.iterator();
iterator.forEachRemaining(System.out::println);

// 結果
// ↓
//aaa
//bbb
//ccc

int lastIndexOf (Object o)

指定された要素がこのリスト内で最後に検出された位置のインデックスを返します。指定された要素がこのリストにない場合は -1を返します。

final var list = List.of("a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]

System.out.println(list.lastIndexOf("")); // -1
System.out.println(list.lastIndexOf("a")); // 3
System.out.println(list.lastIndexOf("b")); // 4
System.out.println(list.lastIndexOf("c")); // 5
System.out.println(list.lastIndexOf("d")); // -1
System.out.println(list.lastIndexOf("abc")); // -1

ListIterator<E> listIterator ()

このリスト内の要素を(適切な順序で)反復するリスト・イテレータを返します。

final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

final var listIterator = list.listIterator();
listIterator.forEachRemaining(System.out::println);

// 結果
// ↓
//aaa
//bbb
//ccc

ListIterator<E> listIterator (int index)

リスト内の指定された位置で始まる、リスト内の要素を(適切な順序で)反復するリスト・イテレータを返します。

final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

{
    final var ret = list.listIterator(0);
    ret.forEachRemaining(System.out::println);

    // 結果
    // ↓
    //aaa
    //bbb
    //ccc
}
{
    final var ret = list.listIterator(1);
    ret.forEachRemaining(System.out::println);

    // 結果
    // ↓
    //bbb
    //ccc
}
{
    final var ret = list.listIterator(2);
    ret.forEachRemaining(System.out::println);

    // 結果
    // ↓
    //ccc
}

static <E> List<E> of ()

ゼロ要素を含む変更不可能なリストを返します。

final var list = List.of();
System.out.println(list); // []
System.out.println(list.size()); // 0

//list.add("a"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1)

1つの要素を含む変更不可能なリストを返します。

final var list = List.of("a");
System.out.println(list); // [a]
System.out.println(list.size()); // 1

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E... elements)

任意の数の要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n");
System.out.println(list); // [a, b, c, d, e, f, g, h, i, j, k, l, m, n]
System.out.println(list.size()); // 14

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2)

2つの要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b");
System.out.println(list); // [a, b]
System.out.println(list.size()); // 2

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2, E e3)

3つの要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]
System.out.println(list.size()); // 3

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2, E e3, E e4)

4つの要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c", "d");
System.out.println(list); // [a, b, c, d]
System.out.println(list.size()); // 4

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2, E e3, E e4, E e5)

5つの要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c", "d", "e");
System.out.println(list); // [a, b, c, d, e]
System.out.println(list.size()); // 5

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2, E e3, E e4, E e5, E e6)

6つの要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c", "d", "e", "f");
System.out.println(list); // [a, b, c, d, e, f]
System.out.println(list.size()); // 6

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7)

7つの要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c", "d", "e", "f", "g");
System.out.println(list); // [a, b, c, d, e, f, g]
System.out.println(list.size()); // 7

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)

8つの要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c", "d", "e", "f", "g", "h");
System.out.println(list); // [a, b, c, d, e, f, g, h]
System.out.println(list.size()); // 8

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)

9つの要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c", "d", "e", "f", "g", "h", "i");
System.out.println(list); // [a, b, c, d, e, f, g, h, i]
System.out.println(list.size()); // 9

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

static <E> List<E> of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)

10個の要素を含む変更不可能なリストを返します。

final var list = List.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
System.out.println(list); // [a, b, c, d, e, f, g, h, i, j]
System.out.println(list.size()); // 10

//list.add("b"); // UnsupportedOperationException
//list.clear(); // UnsupportedOperationException

E remove (int index)

このリスト内の指定された位置にある要素を削除します(オプションの操作)。

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(0)); // a
    System.out.println(list); // [b, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(1)); // b
    System.out.println(list); // [a, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(2)); // c
    System.out.println(list); // [a, b]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    //list.remove(3); // IndexOutOfBoundsException
}

boolean remove (Object o)

指定された要素がこのリストにあれば、その最初のものをリストから削除します(オプションの操作)。

final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]

{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("")); // false
    System.out.println(list); // [a, b, a, b, A, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("a")); // true
    System.out.println(list); // [b, a, b, A, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("b")); // true
    System.out.println(list); // [a, a, b, A, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("A")); // true
    System.out.println(list); // [a, b, a, b, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("B")); // true
    System.out.println(list); // [a, b, a, b, A]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.remove("C")); // false
    System.out.println(list); // [a, b, a, b, A, B]
}

boolean removeAll (Collection<?> c)

このリストから、指定されたコレクションに含まれる要素をすべて削除します(オプションの操作)。

final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]

{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.removeAll(List.of())); // false
    System.out.println(list); // [a, b, a, b, A, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.removeAll(List.of("a"))); // true
    System.out.println(list); // [b, b, A, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.removeAll(List.of("a", "b"))); // true
    System.out.println(list); // [A, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.removeAll(List.of("b", "a"))); // true
    System.out.println(list); // [A, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.removeAll(List.of("A"))); // true
    System.out.println(list); // [a, b, a, b, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.removeAll(List.of("X", "Y", "Z"))); // false
    System.out.println(list); // [a, b, a, b, A, B]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.removeAll(List.of("A", "X", "Y", "Z"))); // true
    System.out.println(list); // [a, b, a, b, B]
}

default void replaceAll (UnaryOperator<E> operator)

このリストの各要素を、その要素に演算子を適用した結果で置換します。

final List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("a");
list.add("b");

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

list.replaceAll(String::toUpperCase);
System.out.println(list); // [A, B, A, B]
final List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);

System.out.println(list); // [1, 2, 3, 4]

list.replaceAll(num -> num * num);
System.out.println(list); // [1, 4, 9, 16]

boolean retainAll (Collection<?> c)

このリスト内で、指定されたコレクションに含まれている要素だけを保持します(オプションの操作)。

final var src = List.of("a", "b", "a", "b", "A", "B");
System.out.println(src); // [a, b, a, b, A, B]

{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.retainAll(List.of())); // true
    System.out.println(list); // []
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.retainAll(List.of("a", "b"))); // true
    System.out.println(list); // [a, b, a, b]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.retainAll(List.of("b", "a"))); // true
    System.out.println(list); // [a, b, a, b]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.retainAll(List.of("A"))); // true
    System.out.println(list); // [A]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.retainAll(List.of("X", "Y", "Z"))); // true
    System.out.println(list); // []
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.retainAll(List.of("A", "X", "Y", "Z"))); // true
    System.out.println(list); // [A]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, a, b, A, B]

    System.out.println(list.retainAll(List.of("a", "b", "A", "B"))); // false
    System.out.println(list); // [a, b, a, b, A, B]
}

E set (int index, E element)

このリスト内の指定された位置にある要素を、指定された要素に置き換えます(オプションの操作)。

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(0, "A")); // a
    System.out.println(list); // [A, b, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(1, "B")); // b
    System.out.println(list); // [a, B, c]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(2, "C")); // c
    System.out.println(list); // [a, b, C]
}
{
    final List<String> list = new ArrayList<>(src);
    System.out.println(list); // [a, b, c]

    //list.set(3, "D"); // IndexOutOfBoundsException
}

int size ()

このリスト内にある要素の数を返します。

final var list1 = List.of();
System.out.println(list1); // []
System.out.println(list1.size()); // 0

final var list2 = List.of("a", "b", "c", "X", "Y", "Z");
System.out.println(list2); // [a, b, c, X, Y, Z]
System.out.println(list2.size()); // 6

default void sort (Comparator<? super E> c)

指定されたComparatorが示す順序に従って、このリストをソートします。

final var words1 = List.of("Tree", "book", "stone", "Cake");
final var words2 = List.of("D", "c", "B", "a");

// 大文字小文字を区別するソート
{
    final var list1 = new ArrayList<>(words1);
    list1.sort(null);
    System.out.println(list1); // [Cake, Tree, book, stone]

    final var list2 = new ArrayList<>(words2);
    list2.sort(null);
    System.out.println(list2); // [B, D, a, c]
}

// 大文字小文字を区別しないソート
{
    final var list1 = new ArrayList<>(words1);
    list1.sort(String.CASE_INSENSITIVE_ORDER);
    System.out.println(list1); // [book, Cake, stone, Tree]

    final var list2 = new ArrayList<>(words2);
    list2.sort(String.CASE_INSENSITIVE_ORDER);
    System.out.println(list2); // [a, B, c, D]
}

default Spliterator<E> spliterator ()

このリスト内の要素に対するSpliteratorを作成します。

final var list = List.of("aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

final var spliterator = list.spliterator();
spliterator.forEachRemaining(System.out::println);

// 結果
// ↓
//aaa
//bbb
//ccc

List<E> subList (int fromIndex, int toIndex)

このリストの、指定されたfromIndex (これを含む)からtoIndex (これを含まない)までの部分のビューを返します。

final List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");

System.out.println(list); // [a, b, c, d]

System.out.println(list.subList(0, 0)); // []
System.out.println(list.subList(0, 1)); // [a]
System.out.println(list.subList(0, 2)); // [a, b]
System.out.println(list.subList(0, 3)); // [a, b, c]
System.out.println(list.subList(0, 4)); // [a, b, c, d]
//list.subList(0, 5); // IndexOutOfBoundsException

// サブリストの変更は、本体のリストにも反映されます。
final var subList = list.subList(1, 3);
System.out.println(subList); // [b, c]
System.out.println(list); // [a, b, c, d]

subList.set(0, "X");
System.out.println(subList); // [X, c]
System.out.println(list); // [a, X, c, d]

subList.add("Y");
System.out.println(subList); // [X, c, Y]
System.out.println(list); // [a, X, c, Y, d]

subList.remove("c");
System.out.println(subList); // [X, Y]
System.out.println(list); // [a, X, Y, d]

Object[] toArray ()

このリスト内のすべての要素を適切な順序で(最初の要素から最後の要素へ)含んでいる配列を返します。

final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

final Object[] array = list.toArray();
System.out.println(Arrays.toString(array)); // [a, b, c]

<T> T[] toArray (T[] a)

このリスト内のすべての要素を適切な順序で(最初の要素から最後の要素へ)含んでいる配列を返します。返される配列の実行時の型は、指定された配列の型になります。

final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

final String[] array = list.toArray(new String[0]);
System.out.println(Arrays.toString(array)); // [a, b, c]
final var list = List.of("a", "b", "c");
System.out.println(list); // [a, b, c]

{
    final String[] array = new String[3];
    System.out.println(Arrays.toString(array)); // [null, null, null]

    final var ret = list.toArray(array);
    System.out.println(Arrays.toString(array)); // [a, b, c]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}
{
    final String[] array = new String[5];
    System.out.println(Arrays.toString(array)); // [null, null, null, null, null]

    final var ret = list.toArray(array);
    System.out.println(Arrays.toString(array)); // [a, b, c, null, null]
    System.out.println(Arrays.toString(ret)); // [a, b, c, null, null]
}
{
    final String[] array = new String[1];
    System.out.println(Arrays.toString(array)); // [null]

    final var ret = list.toArray(array);
    System.out.println(Arrays.toString(array)); // [null]
    System.out.println(Arrays.toString(ret)); // [a, b, c]
}

Collectionで宣言されたメソッド

parallelStream, removeIf, stream, toArray

Java API 使用例 : Collection」をご参照ください。

Iterableで宣言されたメソッド

forEach

Java API 使用例 : Iterable」をご参照ください。


関連記事

ページの先頭へ