# 將列表複製到Java中的另一個列表

## 2.構造函數

``List<Plant> copy = new ArrayList<>(list);``

``List<Integer> copy = new ArrayList<>(list);``

`Integer`是一個不可變的類，其值在創建實例時設置，並且永不更改。

## 3. `List``ConcurrentAccessException`

• 使用專為並發訪問而設計的集合
• 適當地鎖定集合以對其進行迭代
• 找到一種避免複製原始集合的方法

## 4. `AddAll`

``````List<Integer> copy = new ArrayList<>();

## 5. `Collections.copy`

`Collections`類僅由對集合進行操作或返回集合的靜態方法組成。

``````List<Integer> source = Arrays.asList(1,2,3);

List<Integer> dest = Arrays.asList(4,5,6);

Collections.copy(dest, source);``````

``````List<Integer> source = Arrays.asList(1, 2, 3);

List<Integer> dest = Arrays.asList(5, 6, 7, 8, 9, 10);

Collections.copy(dest, source);``````

## 6.使用Java 8

``````List<String> copy = list.stream()

.collect(Collectors.toList());``````

``````List<String> copy = list.stream()

.skip(1)

.collect(Collectors.toList());``````

``````List<String> copy = list.stream()

.filter(s -> s.length() > 10)

.collect(Collectors.toList());``````
``````List<Flower> flowers = list.stream()

.filter(f -> f.getPetals() > 6)

.collect(Collectors.toList());``````

``````List<Flower> flowers = Optional.ofNullable(list)

.map(List::stream)

.orElseGet(Stream::empty)

.collect(Collectors.toList());``````

``````List<Flower> flowers = Optional.ofNullable(list)

.map(List::stream).orElseGet(Stream::empty)

.skip(1)

.collect(Collectors.toList());``````

## 7.使用Java 10

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