From aa2701442567f6efc2a50ef095ef449b836546eb Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Thu, 2 Nov 2023 23:12:45 +0530 Subject: [PATCH] BAEL-7177 Modifying Objects within Stream while iterating --- .../modifystream/ModifyStreamUnitTest.java | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) 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 index 72183ec94a..f5fab408b8 100644 --- 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 @@ -18,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; public class ModifyStreamUnitTest { Logger logger = LoggerFactory.getLogger(ModifyStreamUnitTest.class); - List personLst = new ArrayList(); + List personList = new ArrayList(); List immutablePersonList = new ArrayList(); @BeforeEach @@ -28,10 +28,10 @@ public class ModifyStreamUnitTest { 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); + personList.add(person1); + personList.add(person2); + personList.add(person3); + personList.add(person4); } @BeforeEach @@ -48,32 +48,32 @@ public class ModifyStreamUnitTest { } @Test - void givenPersonLst_whenRemoveWhileIterating_thenThrowException() { + void givenPersonList_whenRemoveWhileIterating_thenThrowException() { assertThrows(NullPointerException.class, () -> { - personLst.stream().forEach(e -> { + personList.stream().forEach(e -> { if(e.getName().equals("John")) { - personLst.remove(e); + personList.remove(e); } }); }); } @Test - void givenPersonLst_whenRemoveWhileIteratingWithForEach_thenThrowException() { + void givenPersonList_whenRemoveWhileIteratingWithForEach_thenThrowException() { assertThrows(ConcurrentModificationException.class, () -> { - personLst.forEach(e -> { + personList.forEach(e -> { if(e.getName().equals("John")) { - personLst.remove(e); + personList.remove(e); } }); }); } @Test - void givenPersonLst_whenRemoveWhileIterating_thenPersonRemoved() { - assertEquals(4, personLst.size()); + void givenPersonList_whenRemoveWhileIterating_thenPersonRemoved() { + assertEquals(4, personList.size()); - CopyOnWriteArrayList cps = new CopyOnWriteArrayList<>(personLst); + CopyOnWriteArrayList cps = new CopyOnWriteArrayList<>(personList); cps.stream().forEach(e -> { if(e.getName().equals("John")) { cps.remove(e); @@ -85,56 +85,56 @@ public class ModifyStreamUnitTest { @Test - void givenPersonLst_whenRemovePersonWithFilter_thenPersonRemoved() { - assertEquals(4, personLst.size()); + void givenPersonList_whenRemovePersonWithFilter_thenPersonRemoved() { + assertEquals(4, personList.size()); - List newPersonLst = personLst.stream() + List newPersonList = personList.stream() .filter(e -> !e.getName().equals("John")) .collect(Collectors.toList()); - assertEquals(3, newPersonLst.size()); + assertEquals(3, newPersonList.size()); } @Test - void givenPersonLst_whenRemovePersonWithRemoveIf_thenPersonRemoved() { - assertEquals(4, personLst.size()); + void givenPersonList_whenRemovePersonWithRemoveIf_thenPersonRemoved() { + assertEquals(4, personList.size()); - personLst.removeIf(e -> e.getName().equals("John")); + personList.removeIf(e -> e.getName().equals("John")); - assertEquals(3, personLst.size()); + assertEquals(3, personList.size()); } @Test - void givenPersonLst_whenUpdatePersonEmailByInterferingWithForEach_thenPersonEmailUpdated() { - personLst.stream().forEach(e -> e.setEmail(e.getEmail().toUpperCase())); + void givenPersonList_whenUpdatePersonEmailByInterferingWithForEach_thenPersonEmailUpdated() { + personList.stream().forEach(e -> e.setEmail(e.getEmail().toUpperCase())); - personLst.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); + personList.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); } @Test - void givenPersonLst_whenUpdatePersonEmailWithMapMethod_thenPersonEmailUpdated() { - List newPersonLst = personLst.stream() + void givenPersonList_whenUpdatePersonEmailWithMapMethod_thenPersonEmailUpdated() { + List newPersonList = personList.stream() .map(e -> new Person(e.getName(), e.getEmail().toUpperCase())) .collect(Collectors.toList()); - newPersonLst.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); + newPersonList.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); } @Test - void givenPersonLst_whenUpdateImmutablePersonEmailWithMapMethod_thenPersonEmailUpdated() { - List newImmutablePersonLst = immutablePersonList.stream() + void givenPersonList_whenUpdateImmutablePersonEmailWithMapMethod_thenPersonEmailUpdated() { + List newImmutablePersonList = immutablePersonList.stream() .map(e -> e.withEmail(e.getEmail().toUpperCase())) .collect(Collectors.toList()); - newImmutablePersonLst.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); + newImmutablePersonList.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); } @Test - void givenPersonLst_whenUpdatePersonEmailByInterferingWithPeek_thenPersonEmailUpdated() { - personLst.stream() + void givenPersonList_whenUpdatePersonEmailByInterferingWithPeek_thenPersonEmailUpdated() { + personList.stream() .peek(e -> e.setEmail(e.getEmail().toUpperCase())) .collect(Collectors.toList()); - personLst.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); + personList.forEach(e -> assertEquals(e.getEmail(), e.getEmail().toUpperCase())); } }