diff --git a/core-java/src/main/java/com/baeldung/generics/Generics.java b/core-java/src/main/java/com/baeldung/generics/Generics.java index ce1325687f..69c7baddd3 100644 --- a/core-java/src/main/java/com/baeldung/generics/Generics.java +++ b/core-java/src/main/java/com/baeldung/generics/Generics.java @@ -1,24 +1,23 @@ +package com.baeldung.generics; + import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class Generics { - // definition of a generic method - public static List fromArrayToList(T[] a) { - List list = new ArrayList<>(); - for (T t : a) { - list.add(t); - } - return list; - } + // definition of a generic method + public static List fromArrayToList(T[] a) { + List list = new ArrayList<>(); + Arrays.stream(a).forEach(list::add); + return list; + } - // example of a generic method that has Number as an upper bound for T - public static List fromArrayToListWithUpperBound(T[] a) { - List list = new ArrayList<>(); - for (T t : a) { - list.add(t); - } - return list; - } + // example of a generic method that has Number as an upper bound for T + public static List fromArrayToListWithUpperBound(T[] a) { + List list = new ArrayList<>(); + Arrays.stream(a).forEach(list::add); + return list; + } } diff --git a/core-java/src/test/java/com/baeldung/generics/GenericsTest.java b/core-java/src/test/java/com/baeldung/generics/GenericsTest.java index 7778f8ea81..9eb459ccb5 100644 --- a/core-java/src/test/java/com/baeldung/generics/GenericsTest.java +++ b/core-java/src/test/java/com/baeldung/generics/GenericsTest.java @@ -1,46 +1,41 @@ -import java.util.Iterator; -import java.util.List; +package com.baeldung.generics; import org.junit.Test; +import java.util.List; + +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.MatcherAssert.assertThat; + public class GenericsTest { - // testing the generic method with Integer - @Test - public void fromArrayToListIntTest() { - Integer[] intArray = { 1, 2, 3, 4, 5 }; - List list = Generics.fromArrayToList(intArray); - Iterator iterator; - iterator = list.iterator(); - while (iterator.hasNext()) { - System.out.println(iterator.next()); - } - } + // testing the generic method with Integer + @Test + public void givenArrayOfIntegers_thanListOfIntegersReturnedOK() { + Integer[] intArray = {1, 2, 3, 4, 5}; + List list = Generics.fromArrayToList(intArray); - // testing the generic method with String - @Test - public void fromArrayToListStringTest() { - String[] stringArray = { "hello1", "hello2", "hello3", "hello4", "hello5" }; - List list = Generics.fromArrayToList(stringArray); - Iterator iterator; - iterator = list.iterator(); - while (iterator.hasNext()) { - System.out.println(iterator.next()); - } - } + assertThat(list, hasItems(intArray)); + } - // testing the generic method with Number as upper bound with Integer - // if we test fromArrayToListWithUpperBound with any type that doesn't - // extend Number it will fail to compile - @Test - public void fromArrayToListUpperboundIntTest() { - Integer[] intArray = { 1, 2, 3, 4, 5 }; - List list = Generics.fromArrayToListWithUpperBound(intArray); - Iterator iterator; - iterator = list.iterator(); - while (iterator.hasNext()) { - System.out.println(iterator.next()); - } - } + // testing the generic method with String + @Test + public void givenArrayOfStrings_thanListOfStringsReturnedOK() { + String[] stringArray = {"hello1", "hello2", "hello3", "hello4", "hello5"}; + List list = Generics.fromArrayToList(stringArray); + + assertThat(list, hasItems(stringArray)); + } + + // testing the generic method with Number as upper bound with Integer + // if we test fromArrayToListWithUpperBound with any type that doesn't + // extend Number it will fail to compile + @Test + public void givenArrayOfIntegersAndNumberUpperBound_thanListOfIntegersReturnedOK() { + Integer[] intArray = {1, 2, 3, 4, 5}; + List list = Generics.fromArrayToListWithUpperBound(intArray); + + assertThat(list, hasItems(intArray)); + } }