3
I’m practicing some new things that came with Java 8 and between those the use of Stream. I have heard 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, so far so good, but let’s 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 personal object have its value changed since it was changed in stream()
that theoretically does not change the value of the actual list?
The person object is with memory reference to the same object on the list, right? Only if the change was made in the stream, it should not have kept the value?
Uhum, I figured so. It was doubtful, as I thought that the stream would never change the value of the list with these actions of for example, Sort, I thought it would also serve for other operations involving the values of the list. Thank you very much
– Paulo Gustavo