Example 1 — Merge Two Lists
Append a list of newcomers to a team and check both lists afterwards.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListAddAllDemo {
public static void main(String[] args) {
List<String> team = new ArrayList<>(Arrays.asList("Ana", "Ben"));
List<String> newcomers = Arrays.asList("Cleo", "Dev");
boolean changed = team.addAll(newcomers);
System.out.println(changed + " " + team);
System.out.println("newcomers untouched: " + newcomers);
}
} How It Works
- Two inputs.
teamis a growableArrayList;newcomersis a fixed-size list fromArrays.asList, used here only as the source of elements. - One call, two adds.
addAllappends Cleo and Dev after Ben, keeping their order. - true means changed. The collection was not empty, so the team grew and the method returned
true. - Input left alone.
addAllonly readsnewcomers, so it still prints[Cleo, Dev].

