Consider this code
List<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee("A", "B"));
employees.add(new Employee("A1", "B1"));
employees.stream().map(e-> { e.setfName("Mr. " + e.getfName()); return e; }); // 1. DOES NOT mutate employees' content
employees.stream().map(e-> { e.setfName("Mr. " + e.getfName()); return e; }).collect(Collectors.toList()); // 2. DOES mutate employees' content
employees.stream().map(e-> new Employee ("Mrs. " + e.getfName(), e.getlName())).collect(Collectors.toList()); // 3. DOES NOT put new objects in original list
Not sure if I understand the reason for this behavior. If the stream does create a separate memory, shouldn't 2 also NOT mutate the original list's content?