BAEL-367 - Simplyfing code
This commit is contained in:
parent
aba015e1ad
commit
4f4782255a
@ -13,9 +13,8 @@ public class Generics {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// definition of a generic method
|
// definition of a generic method
|
||||||
public static <T, G> List<G> fromArrayToList(T[] a, List<G> list, Function<T, G> mapperFunction) {
|
public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
|
||||||
List<T> listWithTypeT = Arrays.stream(a).collect(Collectors.toList());
|
return Arrays.stream(a).map(mapperFunction).collect(Collectors.toList());
|
||||||
return listWithTypeT.stream().map(mapperFunction).collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// example of a generic method that has Number as an upper bound for T
|
// example of a generic method that has Number as an upper bound for T
|
||||||
|
@ -24,8 +24,7 @@ public class GenericsTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenArrayOfIntegers_thanListOfStringReturnedOK() {
|
public void givenArrayOfIntegers_thanListOfStringReturnedOK() {
|
||||||
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
Integer[] intArray = { 1, 2, 3, 4, 5 };
|
||||||
List<String> stringList = new ArrayList<>();
|
List<String> stringList = Generics.fromArrayToList(intArray, Object::toString);
|
||||||
stringList = Generics.fromArrayToList(intArray, stringList, Object::toString);
|
|
||||||
assertThat(stringList, hasItems("1", "2", "3", "4", "5"));
|
assertThat(stringList, hasItems("1", "2", "3", "4", "5"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user