根据这个问题,我们可以修改源,它不称为干扰:
您可以修改流元素本身,不应将其称为“干扰”。
根据这个问题,代码
List<String> list = new ArrayList<>();
list.add("test");
list.forEach(x -> list.add(x));
会扔ConcurrentModificationException。
但我的代码,
Employee[] arrayOfEmps = {
new Employee(1, "Jeff Bezos"),
new Employee(2, "Bill Gates"),
new Employee(3, "hendry cavilg"),
new Employee(4, "mark cuban"),
new Employee(5, "zoe"),
new Employee(6, "billl clinton"),
new Employee(7, "ariana") ,
new Employee(8, "cathre"),
new Employee(9, "hostile"),
new Employee(10, "verner"),
};
Employee el=new Employee(1, "Jeff Bezos");
List<Employee> li=Arrays.asList(arrayOfEmps);
li.stream().map(s->{s.setName("newname");return s;}).forEach(System.out::print);
不会 throw ConcurrentModificationException,即使它实际上改变了源。
而这段代码,
Employee[] arrayOfEmps = {
new Employee(1, "Jeff Bezos"),
new Employee(2, "Bill Gates"),
new Employee(3, "hendry cavilg"),
new Employee(4, "mark cuban"),
new Employee(5, "zoe"),
new Employee(6, "billl clinton"),
new Employee(7, "ariana") ,
new Employee(8, "cathre"),
new Employee(9, "hostile"),
new Employee(10, "verner"),
};
Employee el=new Employee(1, "Jeff Bezos");
List<Employee> li=Arrays.asList(arrayOfEmps);
li.stream().map(s->{s.setName("newname");li.add(s);return s;}).limit(10).forEach(System.out::print);
因此,我不完全了解允许对源进行哪些类型的修改,哪些不允许。看到一个干扰并产生有状态和副作用的流的示例会非常有帮助,并正确指示哪个是哪个。
holdtom
陪伴而非守候
慕尼黑5688855
相关分类