Looking at Java java.util.stream package documentation a doubt arose regarding best practices to follow in streams usage. Considering this code:
HashMap<Integer,Integer> map = new HashMap<>();
map.put(1,1);
map.put(2,2);
map.put(3,3);
map.put(4,4);
map.keySet().parallelStream().forEach(key -> {
if (key == 3) {
map.put(3,0);
}
});
- Is code ouput always equals to ([1,1],[2,2],[3,0],[4,4])?
- Can map.put(3,0) be considered as a non interfering operation?
- Can map.put(3,0) be considered as an accetable side-effect?
In other words, the above code can be considered compliant with the best practices suggested in streams documentation?