Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
.map and mutation of original object's content

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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

employees.stream().map(e-> { e.setfName("Mr. " + e.getfName()); return e; }); // 1. DOES NOT mutate employees' content

Line 1 only doesn't mutate employees' content because it doesn't do anything at all. Streams aren't evaluated until a terminal operation is called.

Line 2 creates a new list to hold the updated employees, but the original employees are updated -- so you have two independently modifiable lists that contain exactly the same employees with exactly the same data where you can't modify one without the other. So with the result of line 2, you could add new Employee objects and not modify employees, but you couldn't modify the employees in that List without modifying the originals too (because they're the same employees).

Only Line 3 creates new Employee objects which exist independently of the originals.

over 4 years ago · Santiago Trujillo Relatório

0

An important thing to understand about streams is that they are lazy. If you map the values, the mapping function is only called when you retrieve values from the stream. collecting a stream is one way to use the values and force the mutations, which is why the second example mutates the list and the first one doesn't. The third example doesn't mutate the objects in place, instead creating a new Employee for each one, so it creates a new cloned list that is unrelated to the original list.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda