From c41bd8085056d0acce634fa8930d7783bc9ae715 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Wed, 10 Jan 2024 02:01:27 +0100 Subject: [PATCH] [call-a-method-oneach] call a method on each element (#15540) * [call-a-method-oneach] call a method on each element * [call-a-method-oneach] fix typo --- .../CallAMethodOnEachElementUnitTest.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/callmethodoneach/CallAMethodOnEachElementUnitTest.java diff --git a/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/callmethodoneach/CallAMethodOnEachElementUnitTest.java b/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/callmethodoneach/CallAMethodOnEachElementUnitTest.java new file mode 100644 index 0000000000..dfd8cb24b5 --- /dev/null +++ b/core-java-modules/core-java-collections-list-6/src/test/java/com/baeldung/callmethodoneach/CallAMethodOnEachElementUnitTest.java @@ -0,0 +1,96 @@ +package com.baeldung.callmethodoneach; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.Function; +import com.google.common.collect.Lists; + +class Player { + + private int id; + private String name; + private int score; + + public Player(int id, String name, int score) { + this.id = id; + this.name = name; + this.score = score; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public int getScore() { + return score; + } +} + +public class CallAMethodOnEachElementUnitTest { + + private Logger log = LoggerFactory.getLogger(this.getClass()); + + // @formatter:off + private final static List PLAYERS = List.of( + new Player(1, "Kai", 42), + new Player(2, "Eric", 43), + new Player(3, "Saajan", 64), + new Player(4, "Kevin", 30), + new Player(5, "John", 5)); + // @formatter:on + + @Test + void whenUsingForLoopCallLogPrint_thenGetExpectedResult() { + for (Player p : PLAYERS) { + log.info(p.getName()); + } + } + + @Test + void whenUsingForEachCallLogPrint_thenGetExpectedResult() { + PLAYERS.forEach(player -> log.info(player.getName())); + } + + @Test + void whenUsingForLoopAndListAddExtractPlayerNames_thenGetExpectedResult() { + List names = new ArrayList<>(); + for (Player p : PLAYERS) { + names.add(p.getName()); + } + assertEquals(Arrays.asList("Kai", "Eric", "Saajan", "Kevin", "John"), names); + } + + @Test + void whenUsingGuavaTransformExtractPlayerNames_thenGetExpectedResult() { + List names = Lists.transform(PLAYERS, new Function() { + @Override + public String apply(Player input) { + return input.getName(); + } + }); + + assertEquals(Arrays.asList("Kai", "Eric", "Saajan", "Kevin", "John"), names); + } + + @Test + void whenUsingStreamMapExtractPlayerNames_thenGetExpectedResult() { + List names = PLAYERS.stream() + .map(player -> player.getName()) + .collect(Collectors.toList()); + assertEquals(List.of("Kai", "Eric", "Saajan", "Kevin", "John"), names); + } + +} \ No newline at end of file