generic argument java code example
Example 1: generic argument java
static void fromArrayToCollection(Object[] a, Collection> c) {
for (Object o : a) {
c.add(o); // compile-time error
}
}
Example 2: java generics
// generic methods
public List fromArrayToList(T[] a) {
return Arrays.stream(a).collect(Collectors.toList());
}
public static List fromArrayToList(T[] a, Function mapperFunction) {
return Arrays.stream(a)
.map(mapperFunction)
.collect(Collectors.toList());
}
// bounded generics
public List fromArrayToList(T[] a) {
...
}
//multiple bounds
// upper bound wildcards
public static void paintAllBuildings(List extends Building> buildings) {
...
}
// lower bound wildcard
super T>