Skip to content

Copy a List to Another List in Java

Published: at 09:09 AMSuggest Changes

Copy a List to Another List in Java

Table of contents

Open Table of contents

1. Constructor

List<T> copy = new ArrayList<>(list);

2. AddAll

List<T> copy = new ArrayList<>();
copy.addAll(list);

3. Collections.copy

List<Integer> source = Arrays.asList(1, 2, 3);
List<Integer> dest = Arrays.asList(5, 6, 7, 8, 9, 10);
Collections.copy(dest, source);

4. Using Stream in Java 8

List<String> copy = list.stream().collect(Collectors.toList());

5. In Java 10

List<T> copy = List.copyOf(list);

Previous Post
Config Spring Boot Multi Module With Jpa
Next Post
使用GitHub Action自动发布Spring Boot的jar包