:
package be.objectsmith;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Playground {
public static void main(String[] args) {
copy(
IntStream.range(1, 20).boxed().collect(Collectors.toSet()),
new HashSet<>(Arrays.asList(2, 5)));
copy(
IntStream.range(1, 5).boxed().collect(Collectors.toSet()),
new HashSet<>(Arrays.asList(2, 5)));
}
private static void copy(Set<Integer> source, Set<Integer> destination) {
source
.stream()
.map(destination::add)
.filter(resultOfAdding -> resultOfAdding)
.limit(10)
.collect(Collectors.toList());
System.out.println("source = " + source);
System.out.println("destination = " + destination);
}
}
wil print:
source = [1, 2, 3, 4]
destination = [1, 2, 3, 4, 5]
source = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
destination = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
, 10 .
, , 10 .