Java stream map modifies custom class object not built in classes

Gagandeep Singh :
class Employee {

    public String name;
    public Integer age;
    public Employee(String n, int age) {
        this.name = n;
        this.age = age;
    }
    public String toString() {
        return this.name+":"+this.age;
    }
}

Inside Main:

ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee("NameA", 10));
list.add(new Employee("NameB", 25));
list.add(new Employee("NameC", 30));
list.add(new Employee("NameD", 45));
list.add(new Employee("NameE", 50));

System.out.println(list);//[NameA:10, NameB:25, NameC:30, NameD:45, NameE:50]

list.stream().filter(e->e.age%10==0).map(e->e.name+="CHANGE").collect(Collectors.toList());

System.out.println(list); //[NameACHANGE:10, NameB:25, NameCCHANGE:30, NameD:45, NameECHANGE:50]


ArrayList<String> strList = new ArrayList<>();
strList.add("1");
strList.add("2");
strList.add("3");
strList.add("4");
strList.add("5");

System.out.println(strList);//[1, 2, 3, 4, 5]

List<String> updatedStrList = strList.stream().map(s->s+="CHANGE").collect(Collectors.toList());

System.out.println(updatedStrList);//[1CHANGE, 2CHANGE, 3CHANGE, 4CHANGE, 5CHANGE]

System.out.println(strList);//[1, 2, 3, 4, 5]

What's the reason for this behaviour? When the Employee object's value is being changed the value gets updated in the original ArrayList but when ArrayList of String was changed the values did not reflect in the original ArrayList.

Eran :

There's a different between e.name+="CHANGE" and s->s+="CHANGE". Both of them create a new String instance, but while the first assigns that new String to an instance variable of an instance of the Employee class (and therefore mutates that instance), the second assigns it to a local String variable.

Therefore e.name+="CHANGE" changes the corresponding instance of the original List and s->s+="CHANGE" doesn't.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=79251&siteId=1