BAEL-376 - simplifying
This commit is contained in:
parent
b4f05d4f6f
commit
5925abc243
@ -3,27 +3,24 @@ package com.baeldung.generics;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Generics {
|
public class Generics {
|
||||||
|
|
||||||
// definition of a generic method
|
// definition of a generic method
|
||||||
public static <T> List<T> fromArrayToList(T[] a) {
|
public static <T> List<T> fromArrayToList(T[] a) {
|
||||||
List<T> list = new ArrayList<>();
|
return Arrays.stream(a).collect(Collectors.toList());
|
||||||
Arrays.stream(a).forEach(list::add);
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
||||||
List<T> list = new ArrayList<>();
|
return Arrays.stream(a).collect(Collectors.toList());
|
||||||
Arrays.stream(a).forEach(list::add);
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// example of a generic method with a wild card, this method can be used
|
// example of a generic method with a wild card, this method can be used
|
||||||
// with a list of any subtype of Building
|
// with a list of any subtype of Building
|
||||||
public static boolean paintAllBuildings(List<? extends Building> buildings) {
|
public static boolean paintAllBuildings(List<? extends Building> buildings) {
|
||||||
buildings.stream().forEach(Building::paint);
|
buildings.forEach(Building::paint);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user