Example of non-interference in java 8

  • A+
Category:Languages

Hi according to this question we can modify the source and its not called interference

you can modify the stream elements themselves and it should not be called as "interference".

according to this question

List<String> list = new ArrayList<>();   list.add("test");   list.forEach(x -> list.add(x)); 

will throw ConcurrentModificationException

but my code

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

doesn't throw concurrent modification,eventhough it infact changes the source.

and this code

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

throws

Exception in thread "main" java.lang.UnsupportedOperationException     at java.util.AbstractList.add(Unknown Source)     at java.util.AbstractList.add(Unknown Source)     at java8.Streams.lambda$0(Streams.java:33)     at java.util.stream.ReferencePipeline$3$1.accept(Unknown Source)     at java.util.Spliterators$ArraySpliterator.forEachRemaining(Unknown Source)     at java.util.stream.AbstractPipeline.copyInto(Unknown Source)     at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)     at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(Unknown Source)     at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(Unknown Source)     at java.util.stream.AbstractPipeline.evaluate(Unknown Source)     at java.util.stream.ReferencePipeline.forEach(Unknown Source) 

so i dont exactly understand what type of modifications are allowed to source and what are not . it would be very helpful to see an example which interfers and have a statefull and side-effect producing stream. with proper indication that which-is-which

 


When you do this:

li.stream().map(s->{s.setName("newname");return s;})

you didn't alter the list itself but an element within this list; so it doesn't trigger a ConcurrentModificationException as you expected.

In the last code snippet, you are using Array.asList.
You have to know that Array.asList returns a mere read-only wrapper over an array (a specific inner ArrayList class) explaining why add is not supported.

Indeed this inner class does not override AbstractList#add method by design; causing UnsupportedOperationException; and still not ConcurrentModificationException as you expected again.

Here an example similar to your last snippet that does throw a ConcurrentModificationException:

public static void main(String[] args) {     Employee[] arrayOfEmps = {       new Employee(1, "Jeff Bezos")     };     Employee el = new Employee(11, "Bill Gates");     List<Employee> li = new ArrayList<>(Arrays.asList(arrayOfEmps)); // to avoid read-only restriction     li.stream().peek(s -> li.add(el)).forEach(System.out::print); }  

Note that I'm wrapping the Arrays.List return with a "true" ArrayList, allowing writing because this one implements the add method ;)

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: