Skip to content

java collection operations

gluehloch edited this page Nov 1, 2021 · 3 revisions

Java Collections Must

Copy a List to another

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

Achtung: Falls die Elemente der Liste veränderlich sind, hat eine Änderung an einem Element Auswirkungen auf beide Listen.

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);

Clone this wiki locally