Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Wildcard capture in Java

A private `<T>` helper lets you swap two elements of the same `List<?>` without a cast or exposing the element type in the public method signature.

Version: Java SE 21

Syntax

public static void swapFirst(List<?> list) {
    swapFirstCaptured(list);
}

private static <T> void swapFirstCaptured(List<T> list) {
    if (list.size() < 2) {
        return;
    }
    T first = list.get(0);
    list.set(0, list.get(1));
    list.set(1, first);
}

In Java SE 21, List<?> is a list with an unknown type argument. You can read an element as Object, but you cannot put an arbitrary Object into the list: it could be a List<String>. It is not a List<Object>, and its elements need not all have the same runtime class.

The pattern “Wildcard capture helper List<?> -> List<T>” means passing the same list to a generic method, not converting it to a different list. At swapFirstCaptured(list), capture conversion gives ? a fresh type variable, and type inference uses that variable for T in this call. This is capture conversion through <T> helper: the declaration <T> before void gives the helper one consistent name for the type that get returns and set accepts. Writing list.set(0, list.get(1)) directly in the List<?> method fails to compile because the compiler cannot match the type of the retrieved value to the type required by set. Capture happens at compile time: no copy, cast, or runtime inspection of element classes is needed.

swapFirst accepts List<?> and modifies that same list. With fewer than two elements it does nothing. Otherwise the list must support set; if it does not, the swap throws UnsupportedOperationException. If the library operation is all you need, Collections.swap(List<?>, int, int) already accepts List<?>: callers need no helper of their own, but still need valid indices and support for set.

Examples

import java.util.ArrayList;
import java.util.List;

public class WildcardCaptureDemo {
    public static void swapFirst(List<?> list) {
        swapFirstCaptured(list);
    }

    private static <T> void swapFirstCaptured(List<T> list) {
        if (list.size() < 2) {
            return;
        }
        T first = list.get(0);
        list.set(0, list.get(1));
        list.set(1, first);
    }

    public static void main(String[] args) {
        List<String> words = new ArrayList<>(List.of("red", "blue"));
        List<Integer> numbers = new ArrayList<>(List.of(10, 20));

        swapFirst(words);
        swapFirst(numbers);

        System.out.println(words.get(0) + ", " + words.get(1));
        System.out.println(numbers.get(0) + ", " + numbers.get(1));
    }
}

Result

blue, red
20, 10

The same `swapFirst` method swaps elements in a modifiable list of strings and a modifiable list of integers. The two lines show the order after each swap.

Related lessons

  • Java · Lesson 12 · Advanced · Version: 2
  • Java · Lesson 34 · Intermediate · Version: 2

Sources