java8 stream是可以過程中向物件中放東西的
public static void main(String[] args) { List<Person> list = new ArrayList<>(); Person person; for (int i = 0; i < 20; i++) { person = new Person(); person.setName("person :" + String.valueOf(i)); person.setAge(i); person.
setHeight(Long.valueOf(i)); person.setWeight(Double.valueOf(i)); list.add(person); } list.parallelStream().forEach(father -> { Person child = new Person(); child.setName("child"); child.setAge(9); child.setHeight((long) 1.2); child.setWeight(59.00); List<Person> children = new ArrayList<>(); children.add(child); father.setChildren(children); }); list.spliterator().forEachRemaining(per -> System.out.println(per.getChildren()));
輸出 [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}] [Person{name=‘child’, age=9, height=1, weight=59.0, children=null}]
明顯是放進去了