From efad3fe07fc7a9cac2a06f0ba241fb1fe35e3124 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Thu, 22 Dec 2022 22:23:29 +0100 Subject: [PATCH] [BAEL-6051_intListToStrList] convert List to List (#13169) --- .../IntListToStringListUnitTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/intlisttostrlist/IntListToStringListUnitTest.java diff --git a/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/intlisttostrlist/IntListToStringListUnitTest.java b/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/intlisttostrlist/IntListToStringListUnitTest.java new file mode 100644 index 0000000000..0990ef2b74 --- /dev/null +++ b/core-java-modules/core-java-collections-conversions-2/src/test/java/com/baeldung/intlisttostrlist/IntListToStringListUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.intlisttostrlist; + + +import com.google.common.base.Functions; +import com.google.common.collect.Lists; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class IntListToStringListUnitTest { + private final static List INTEGER_LIST = Arrays.asList(1, 2, 3, 4, 5, 6, 7); + private final static List EXPECTED_LIST = Arrays.asList("1", "2", "3", "4", "5", "6", "7"); + + + @Test + void givenAnIntegerList_whenUsingStreamMap_shouldGetExpectedStringList() { + List result = INTEGER_LIST.stream().map(i -> i.toString()).collect(Collectors.toList()); + assertEquals(EXPECTED_LIST, result); + } + + @Test + void givenAnIntegerList_whenUsingGuava_shouldGetExpectedStringList() { + List result = Lists.transform(INTEGER_LIST, Functions.toStringFunction()); + assertEquals(EXPECTED_LIST, result); + } + + @Test + void givenAnIntegerList_whenUsingLoop_shouldGetExpectedStringList() { + List result = new ArrayList<>(); + for (Integer i : INTEGER_LIST) { + result.add(i.toString()); + } + assertEquals(EXPECTED_LIST, result); + } +}