I'm practicing some new things that came with Java 8 and among those using Stream. I have heard in a place that when we do some action in a list using the stream, it does not change the value of the original list, for example:
List<String> lista = Arrays.asList("a", "c", "b");
lista.stream().sorted(Collector.comparing(String::toString)).forEach(System.out::println);
//saída
//a, b, c
lista.forEach(System.out::println);
//saída
//a, c, b
Well, that's fine, but let's just say I have this scenario:
import java.util.ArrayList;
import java.util.List;
public class Teste {
public static void main(String[] args) {
List<Pessoa> lista = new ArrayList<Teste.Pessoa>();
lista.add(new Pessoa("Paulo", "Gustavo"));
lista.add(new Pessoa("Bla", "Ble"));
Pessoa pessoa = lista.get(0);
System.out.println(pessoa);
mudaValor(lista);
System.out.println(pessoa);
}
public static void mudaValor(List<Pessoa> lista) {
lista.stream().forEach(pessoa -> {
pessoa.setNome("Joquino");
});
}
static class Pessoa {
private String nome;
private String sobreNome;
public Pessoa(String nome, String sobreNome) {
this.nome = nome;
this.sobreNome = sobreNome;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSobreNome() {
return sobreNome;
}
public void setSobreNome(String sobreNome) {
this.sobreNome = sobreNome;
}
@Override
public String toString() {
return nome;
}
}
}
My question is: why did my person object have its value changed since it was changed in stream()
which theoretically does not change the value of the real list?
The person object is referenced from the same memory object list, right? But if the change was made to the stream, should not it have kept the value?