2017-05-14 1 views

Répondre

4

Sûr. Mais si vous avez une collection, vous pouvez utiliser forEach et lambda:

someCollectionContainingTheIntegers.forEach(id -> someClass.getList().add(new Whatever(param1, id)); 

Une autre variante possible consiste à recueillir dans la liste de destination:

someCollectionContainingTheIntegers.stream() 
    .map(id -> new Whatever(param1, id)) 
    .collect(Collectors.toCollection(() -> someClass.getList())); 
+0

Merci beaucoup, Janos :) –

1

faire un foreach dans la liste

List<Integer> ml = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); 
List<Integer> ml2 = Arrays.asList(21, 22, 23, 24); 
ml2.forEach(x -> ml.add(x)); 
System.out.println(ml); 
2

Une solution plus ...

List<Whatever> collect = someCollectionContainingTheIntegers.stream() 
      .map(id -> new Whatever(param1, id)) 
      .collect(toList()); 
    someClass.getList().addAll(collect);