added more example
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
|
||||
public class Building {
|
||||
public void paint() {
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Generics {
|
||||
@@ -6,19 +7,24 @@ public class Generics {
|
||||
// definition of a generic method
|
||||
public static <T> List<T> fromArrayToList(T[] a) {
|
||||
List<T> list = new ArrayList<>();
|
||||
for (T t : a) {
|
||||
list.add(t);
|
||||
}
|
||||
Arrays.stream(a).forEach(list::add);
|
||||
return list;
|
||||
}
|
||||
|
||||
// example of a generic method that has Number as an upper bound for T
|
||||
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
|
||||
List<T> list = new ArrayList<>();
|
||||
for (T t : a) {
|
||||
list.add(t);
|
||||
}
|
||||
Arrays.stream(a).forEach(list::add);
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
// example of a generic method with a wild card, this method can be used
|
||||
// with a list of any subtype of Building
|
||||
public static boolean paintAllBuildings(List<? extends Building> buildings) {
|
||||
for (Building building : buildings) {
|
||||
building.paint();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
|
||||
public class SubBuilding extends Building {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user