From a5d58617e2aac4e7605914a3f771d1a6617d9e7f Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Thu, 2 Nov 2023 20:21:59 +0530 Subject: [PATCH] BAEL-7177 Modifying Objects within Stream while iterating --- .../modifystream/entity/ImmutablePerson.java | 25 ++++ .../baledung/modifystream/entity/Person.java | 27 ++++ .../modifystream/ModifyStreamUnitTest.java | 140 ++++++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 core-java-modules/core-java-streams-5/src/main/java/com/baledung/modifystream/entity/ImmutablePerson.java create mode 100644 core-java-modules/core-java-streams-5/src/main/java/com/baledung/modifystream/entity/Person.java create mode 100644 core-java-modules/core-java-streams-5/src/test/java/com/baeldung/modifystream/ModifyStreamUnitTest.java diff --git a/core-java-modules/core-java-streams-5/src/main/java/com/baledung/modifystream/entity/ImmutablePerson.java b/core-java-modules/core-java-streams-5/src/main/java/com/baledung/modifystream/entity/ImmutablePerson.java new file mode 100644 index 0000000000..bb7f3b8eb3 --- /dev/null +++ b/core-java-modules/core-java-streams-5/src/main/java/com/baledung/modifystream/entity/ImmutablePerson.java @@ -0,0 +1,25 @@ +package com.baledung.modifystream.entity; + +public class ImmutablePerson { + + private String name; + private String email; + + public ImmutablePerson(String name, String email) { + this.name = name; + this.email = email; + } + + public ImmutablePerson withEmail(String email) { + + return new ImmutablePerson(this.name, email); + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } +} diff --git a/core-java-modules/core-java-streams-5/src/main/java/com/baledung/modifystream/entity/Person.java b/core-java-modules/core-java-streams-5/src/main/java/com/baledung/modifystream/entity/Person.java new file mode 100644 index 0000000000..c82c159059 --- /dev/null +++ b/core-java-modules/core-java-streams-5/src/main/java/com/baledung/modifystream/entity/Person.java @@ -0,0 +1,27 @@ +package com.baledung.modifystream.entity; + +public class Person { + private String name; + private String email; + + public Person(String name, String email) { + this.name = name; + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/modifystream/ModifyStreamUnitTest.java b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/modifystream/ModifyStreamUnitTest.java new file mode 100644 index 0000000000..72183ec94a --- /dev/null +++ b/core-java-modules/core-java-streams-5/src/test/java/com/baeldung/modifystream/ModifyStreamUnitTest.java @@ -0,0 +1,140 @@ +package com.baeldung.modifystream; + +import com.baledung.modifystream.entity.ImmutablePerson; +import com.baledung.modifystream.entity.Person; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.ConcurrentModificationException; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ModifyStreamUnitTest { + Logger logger = LoggerFactory.getLogger(ModifyStreamUnitTest.class); + List personLst = new ArrayList(); + List immutablePersonList = new ArrayList(); + + @BeforeEach + void prepare() { + Person person1 = new Person("John", "john@gmail.com"); + Person person2 = new Person("Peter", "peter@gmail.com"); + Person person3 = new Person("Mary", "mary@gmail.com"); + Person person4 = new Person("William", "william@gmail.com"); + + personLst.add(person1); + personLst.add(person2); + personLst.add(person3); + personLst.add(person4); + } + + @BeforeEach + void prepareImmutablePerson() { + ImmutablePerson immutablePerson1 = new ImmutablePerson("John", "john@gmail.com"); + ImmutablePerson immutablePerson2 = new ImmutablePerson("Peter", "peter@gmail.com"); + ImmutablePerson immutablePerson3 = new ImmutablePerson("Mary", "mary@gmail.com"); + ImmutablePerson immutablePerson4 = new ImmutablePerson("William", "william@gmail.com"); + + immutablePersonList.add(immutablePerson1); + immutablePersonList.add(immutablePerson2); + immutablePersonList.add(immutablePerson3); + immutablePersonList.add(immutablePerson4); + } + + @Test + void givenPersonLst_whenRemoveWhileIterating_thenThrowException() { + assertThrows(NullPointerException.class, () -> { + personLst.stream().forEach(e -> { + if(e.getName().equals("John")) { + personLst.remove(e); + } + }); + }); + } + + @Test + void givenPersonLst_whenRemoveWhileIteratingWithForEach_thenThrowException() { + assertThrows(ConcurrentModificationException.class, () -> { + personLst.forEach(e -> { + if(e.getName().equals("John")) { + personLst.remove(e); + } + }); + }); + } + + @Test + void givenPersonLst_whenRemoveWhileIterating_thenPersonRemoved() { + assertEquals(4, personLst.size()); + + CopyOnWriteArrayList cps = new CopyOnWriteArrayList<>(personLst); + cps.stream().forEach(e -> { + if(e.getName().equals("John")) { + cps.remove(e); + } + }); + + assertEquals(3, cps.size()); + } + + + @Test + void givenPersonLst_whenRemovePersonWithFilter_thenPersonRemoved() { + assertEquals(4, personLst.size()); + + List newPersonLst = personLst.stream() + .filter(e -> !e.getName().equals("John")) + .collect(Collectors.toList()); + + assertEquals(3, newPersonLst.size()); + } + + @Test + void givenPersonLst_whenRemovePersonWithRemoveIf_thenPersonRemoved() { + assertEquals(4, personLst.size()); + + personLst.removeIf(e -> e.getName().equals("John")); + + assertEquals(3, personLst.size()); + } + + @Test + void givenPersonLst_whenUpdatePersonEmailByInterferingWithForEach_thenPersonEmailUpdated() { + personLst.stream().forEach(e -> e.setEmail(e.getEmail().toUpperCase())); + + personLst.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); + } + + @Test + void givenPersonLst_whenUpdatePersonEmailWithMapMethod_thenPersonEmailUpdated() { + List newPersonLst = personLst.stream() + .map(e -> new Person(e.getName(), e.getEmail().toUpperCase())) + .collect(Collectors.toList()); + + newPersonLst.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); + } + + @Test + void givenPersonLst_whenUpdateImmutablePersonEmailWithMapMethod_thenPersonEmailUpdated() { + List newImmutablePersonLst = immutablePersonList.stream() + .map(e -> e.withEmail(e.getEmail().toUpperCase())) + .collect(Collectors.toList()); + + newImmutablePersonLst.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); + } + @Test + void givenPersonLst_whenUpdatePersonEmailByInterferingWithPeek_thenPersonEmailUpdated() { + personLst.stream() + .peek(e -> e.setEmail(e.getEmail().toUpperCase())) + .collect(Collectors.toList()); + + personLst.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); + } + +}