@jaylen.trantow
В Java можно добавить коллекцию в другую коллекцию, используя методы addAll() или Iterator.
1 2 3 |
Collection<Integer> collection1 = new ArrayList<>(); Collection<Integer> collection2 = new HashSet<>(); collection1.addAll(collection2); |
В приведенном примере мы добавляем все элементы из collection2
в collection1
с помощью метода addAll()
.
1 2 3 4 5 6 |
Collection<Integer> collection1 = new ArrayList<>(); Collection<Integer> collection2 = new HashSet<>(); Iterator<Integer> iterator = collection2.iterator(); while(iterator.hasNext()){ collection1.add(iterator.next()); } |
Этот пример показывает, как с использованием итератора можно добавить каждый элемент из collection2
в collection1
с использованием метода add()
.