Java : CopyOnWriteArrayList with Examples

CopyOnWriteArrayList (Java SE 19 & JDK 19) API Examples.
You will find code examples on most CopyOnWriteArrayList<E> methods.


Summary

A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

Class diagram

void test(List<Integer> list) throws InterruptedException {
    for (int i = 0; i < 5; i++) {
        list.add(i);
    }

    try (final var executor = Executors.newSingleThreadScheduledExecutor()) {

        executor.schedule(() -> {
            System.out.println("-- add value! --");
            list.add(9999);
        }, 5, TimeUnit.SECONDS);

        try {
            for (final var value : list) {
                System.out.println("value = " + value);
                TimeUnit.SECONDS.sleep(2);
            }
        } catch (ConcurrentModificationException e) {
            System.out.println("ConcurrentModificationException!");
        }
    }

    System.out.println("-- end --");
    System.out.println("list = " + list);
}
test(new ArrayList<>());

// Result
// ↓
//value = 0
//value = 1
//value = 2
//-- add value! --
//ConcurrentModificationException!
//-- end --
//list = [0, 1, 2, 3, 4, 9999]

test(new CopyOnWriteArrayList<>());

// Result
// ↓
//value = 0
//value = 1
//value = 2
//-- add value! --
//value = 3
//value = 4
//-- end --
//list = [0, 1, 2, 3, 4, 9999]

Constructors

CopyOnWriteArrayList ()

Creates an empty list.

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

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

CopyOnWriteArrayList (E[] toCopyIn)

Creates a list holding a copy of the given array.

final String[] toCopyIn = {"aaa", "bbb", "ccc"};
System.out.println(Arrays.toString(toCopyIn)); // [aaa, bbb, ccc]

final var list = new CopyOnWriteArrayList<>(toCopyIn);
System.out.println(list); // [aaa, bbb, ccc]

CopyOnWriteArrayList (Collection<? extends E> c)

Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

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

final var list = new CopyOnWriteArrayList<>(c);
System.out.println(list); // [aaa, bbb, ccc]

Methods

void add (int index, E element)

Inserts the specified element at the specified position in this list.

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

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

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

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

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

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

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

boolean add (E e)

Appends the specified element to the end of this list.

final var list = new CopyOnWriteArrayList<String>();
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)

Inserts all of the elements in the specified collection into this list, starting at the specified position.

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

{
    final var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(src);

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)

Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.

final var list = new CopyOnWriteArrayList<String>();

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 var list = new CopyOnWriteArrayList<String>();
System.out.println(list); // []

System.out.println(list.addAll(Set.of("c1", "c2", "c3"))); // true
System.out.println(list); // [c3, c2, c1]

int addAllAbsent (Collection<? extends E> c)

Appends all of the elements in the specified collection that are not already contained in this list, to the end of this list, in the order that they are returned by the specified collection's iterator.

final var list = new CopyOnWriteArrayList<>();
System.out.println(list); // []

final var ret1 = list.addAllAbsent(List.of("aaa"));
System.out.println(ret1); // 1
System.out.println(list); // [aaa]

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

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

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

boolean addIfAbsent (E e)

Appends the element, if not present.

final var list = new CopyOnWriteArrayList<>();
System.out.println(list); // []

final var ret1 = list.addIfAbsent("aaa");
System.out.println(ret1); // true
System.out.println(list); // [aaa]

final var ret2 = list.addIfAbsent("XXX");
System.out.println(ret2); // true
System.out.println(list); // [aaa, XXX]

final var ret3 = list.addIfAbsent("aaa");
System.out.println(ret3); // false
System.out.println(list); // [aaa, XXX]

final var ret4 = list.addIfAbsent("YYY");
System.out.println(ret4); // true
System.out.println(list); // [aaa, XXX, YYY]

void clear ()

Removes all of the elements from this list.

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

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

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

Object clone ()

Returns a shallow copy of this list.

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

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

final var cloned = list.clone();
System.out.println(cloned); // [a, b, c]
System.out.println(cloned.getClass()); // class java.util.concurrent.CopyOnWriteArrayList

boolean contains (Object o)

Returns true if this list contains the specified element.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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)

Returns true if this list contains all of the elements of the specified collection.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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

boolean equals (Object o)

Compares the specified object with this list for equality.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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 = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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 = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

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

void forEach (Consumer<? super E> action)

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

System.out.println("-- forEach --");
list.forEach(element -> {
    System.out.println(element);
});

// Result
// ↓
//-- forEach --
//aaa
//bbb
//ccc

E get (int index)

Returns the element at the specified position in this list.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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 = new CopyOnWriteArrayList<Integer>();
Collections.addAll(list, 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 ()

Returns the hash code value for this list.

final var list1 = new CopyOnWriteArrayList<String>();
Collections.addAll(list1, "a", "b", "c");
System.out.println(list1.hashCode()); // 126145

final var list2 = new CopyOnWriteArrayList<String>();
Collections.addAll(list2, "A", "B", "C");
System.out.println(list2.hashCode()); // 94369

final var list3 = new CopyOnWriteArrayList<Integer>();
Collections.addAll(list3, 1, 2, 3);
System.out.println(list3.hashCode()); // 30817

int indexOf (E e, int index)

Returns the index of the first occurrence of the specified element in this list, searching forwards from index, or returns -1 if the element is not found.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]

System.out.println(list.indexOf("a", 0)); // 0
System.out.println(list.indexOf("a", 1)); // 3
System.out.println(list.indexOf("a", 2)); // 3
System.out.println(list.indexOf("a", 3)); // 3
System.out.println(list.indexOf("a", 4)); // -1
System.out.println(list.indexOf("a", 5)); // -1

System.out.println(list.indexOf("b", 0)); // 1
System.out.println(list.indexOf("b", 1)); // 1
System.out.println(list.indexOf("b", 2)); // 4
System.out.println(list.indexOf("b", 3)); // 4
System.out.println(list.indexOf("b", 4)); // 4
System.out.println(list.indexOf("b", 5)); // -1

System.out.println(list.indexOf("c", 0)); // 2
System.out.println(list.indexOf("c", 1)); // 2
System.out.println(list.indexOf("c", 2)); // 2
System.out.println(list.indexOf("c", 3)); // 5
System.out.println(list.indexOf("c", 4)); // 5
System.out.println(list.indexOf("c", 5)); // 5

System.out.println(list.indexOf("d", 0)); // -1

int indexOf (Object o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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 ()

Returns true if this list contains no elements.

final var list = new CopyOnWriteArrayList<String>();
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 ()

Returns an iterator over the elements in this list in proper sequence.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

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

// Result
// ↓
//aaa
//bbb
//ccc

int lastIndexOf (E e, int index)

Returns the index of the last occurrence of the specified element in this list, searching backwards from index, or returns -1 if the element is not found.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "a", "b", "c", "a", "b", "c");
System.out.println(list); // [a, b, c, a, b, c]

System.out.println(list.lastIndexOf("a", 0)); // 0
System.out.println(list.lastIndexOf("a", 1)); // 0
System.out.println(list.lastIndexOf("a", 2)); // 0
System.out.println(list.lastIndexOf("a", 3)); // 3
System.out.println(list.lastIndexOf("a", 4)); // 3
System.out.println(list.lastIndexOf("a", 5)); // 3

System.out.println(list.lastIndexOf("b", 0)); // -1
System.out.println(list.lastIndexOf("b", 1)); // 1
System.out.println(list.lastIndexOf("b", 2)); // 1
System.out.println(list.lastIndexOf("b", 3)); // 1
System.out.println(list.lastIndexOf("b", 4)); // 4
System.out.println(list.lastIndexOf("b", 5)); // 4

System.out.println(list.lastIndexOf("c", 0)); // -1
System.out.println(list.lastIndexOf("c", 1)); // -1
System.out.println(list.lastIndexOf("c", 2)); // 2
System.out.println(list.lastIndexOf("c", 3)); // 2
System.out.println(list.lastIndexOf("c", 4)); // 2
System.out.println(list.lastIndexOf("c", 5)); // 5

System.out.println(list.lastIndexOf("d", 5)); // -1

int lastIndexOf (Object o)

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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 ()

Returns a list iterator over the elements in this list (in proper sequence).

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

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

// Result
// ↓
//aaa
//bbb
//ccc

ListIterator<E> listIterator (int index)

Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

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

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

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

    // Result
    // ↓
    //ccc
}

E remove (int index)

Removes the element at the specified position in this list.

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

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

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

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

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

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

boolean remove (Object o)

Removes the first occurrence of the specified element from this list, if it is present.

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

{
    final var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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)

Removes from this list all of its elements that are contained in the specified collection.

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

{
    final var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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]
}

boolean removeIf (Predicate<? super E> filter)

Removes all of the elements of this collection that satisfy the given predicate.

final var list = new CopyOnWriteArrayList<String>();
list.add("aaa");
list.add("BBB");
list.add("ccc");
list.add("DDD");

System.out.println(list); // [aaa, BBB, ccc, DDD]

final var ret = list.removeIf(s -> {
    return s.equals(s.toUpperCase());
});

System.out.println(ret); // true
System.out.println(list); // [aaa, ccc]
final var list = new CopyOnWriteArrayList<String>();
list.add("aaa");
list.add("bbb");

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

final var ret = list.removeIf(s -> s.equals(s.toUpperCase()));

System.out.println(ret); // false
System.out.println(list); // [aaa, bbb]

boolean retainAll (Collection<?> c)

Retains only the elements in this list that are contained in the specified collection.

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

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

    System.out.println(list.retainAll(List.of())); // true
    System.out.println(list); // []
}
{
    final var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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 var list = new CopyOnWriteArrayList<>(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)

Replaces the element at the specified position in this list with the specified element.

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

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

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

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

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

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

int size ()

Returns the number of elements in this list.

final var list = new CopyOnWriteArrayList<String>();
System.out.println(list); // []
System.out.println(list.size()); // 0

list.add("a");

System.out.println(list); // [a]
System.out.println(list.size()); // 1

list.add("b");
list.add("c");
list.add("d");

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

Spliterator<E> spliterator ()

Returns a Spliterator over the elements in this list.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

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

// Result
// ↓
//aaa
//bbb
//ccc

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

Returns a view of the portion of this list between fromIndex, inclusive, and toIndex, exclusive.

final var list = new CopyOnWriteArrayList<String>();
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 ()

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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)

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

final var list = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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 = new CopyOnWriteArrayList<String>();
Collections.addAll(list, "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]
}

String toString ()

Returns a string representation of this list.

final var list = new CopyOnWriteArrayList<String>();

final var str1 = list.toString();
System.out.println(str1); // []

list.add("aaa");

final var str2 = list.toString();
System.out.println(str2); // [aaa]

list.add("bbb");

final var str3 = list.toString();
System.out.println(str3); // [aaa, bbb]

list.add("ccc");

final var str4 = list.toString();
System.out.println(str4); // [aaa, bbb, ccc]

Methods declared in Collection

parallelStream, stream, toArray

Please see the link below.

Methods declared in List

replaceAll, sort

Please see the link below.


Related posts

To top of page