[modify-print-list] renaming lambda parameters, removing filter()

This commit is contained in:
Kai.Yuan 2024-02-28 10:47:00 +08:00
parent 0648a4f918
commit 2af9a69132
1 changed files with 9 additions and 11 deletions

View File

@ -18,14 +18,14 @@ public class ModifyAndPrintListElementsUnitTest {
@Test @Test
void whenPrintingInForEach_thenListIsPrinted() { void whenPrintingInForEach_thenListIsPrinted() {
List<String> theList = Lists.newArrayList("Kai", "Liam", "Eric", "Kevin"); List<String> theList = Lists.newArrayList("Kai", "Liam", "Eric", "Kevin");
theList.forEach(e -> log.info(e)); theList.forEach(element -> log.info(element));
} }
@Test @Test
void whenUsingModifyAndPrintingSeparately_thenListIsModifiedAndPrinted() { void whenUsingModifyAndPrintingSeparately_thenListIsModifiedAndPrinted() {
List<String> theList = Lists.newArrayList("Kai", "Liam", "Eric", "Kevin"); List<String> theList = Lists.newArrayList("Kai", "Liam", "Eric", "Kevin");
theList.replaceAll(e -> e.toUpperCase()); theList.replaceAll(element -> element.toUpperCase());
theList.forEach(e -> log.info(e)); theList.forEach(element -> log.info(element));
assertEquals(List.of("KAI", "LIAM", "ERIC", "KEVIN"), theList); assertEquals(List.of("KAI", "LIAM", "ERIC", "KEVIN"), theList);
} }
@ -33,24 +33,22 @@ public class ModifyAndPrintListElementsUnitTest {
void whenPrintingInMap_thenStreamIsModifiedAndPrinted() { void whenPrintingInMap_thenStreamIsModifiedAndPrinted() {
List<String> theList = List.of("Kai", "Liam", "Eric", "Kevin"); List<String> theList = List.of("Kai", "Liam", "Eric", "Kevin");
List<String> newList = theList.stream() List<String> newList = theList.stream()
.map(e -> { .map(element -> {
String newElement = e.toUpperCase(); String newElement = element.toUpperCase();
log.info(newElement); log.info(newElement);
return newElement; return newElement;
}) })
.filter(e -> e.length() == 4)
.collect(Collectors.toList()); .collect(Collectors.toList());
assertEquals(List.of("LIAM", "ERIC"), newList); assertEquals(List.of("KAI", "LIAM", "ERIC", "KEVIN"), newList);
} }
@Test @Test
void whenPrintingInPeek_thenStreamIsModifiedAndPrinted() { void whenPrintingInPeek_thenStreamIsModifiedAndPrinted() {
List<String> theList = List.of("Kai", "Liam", "Eric", "Kevin"); List<String> theList = List.of("Kai", "Liam", "Eric", "Kevin");
List<String> newList = theList.stream() List<String> newList = theList.stream()
.map(e -> e.toUpperCase()) .map(element -> element.toUpperCase())
.peek(e -> log.info(e)) .peek(element-> log.info(element))
.filter(e -> e.length() == 4)
.collect(Collectors.toList()); .collect(Collectors.toList());
assertEquals(List.of("LIAM", "ERIC"), newList); assertEquals(List.of("KAI", "LIAM", "ERIC", "KEVIN"), newList);
} }
} }