added more example

This commit is contained in:
Yasser Afifi
2016-12-02 23:31:10 +00:00
parent 6835625602
commit b18e801370
4 changed files with 51 additions and 27 deletions

View File

@@ -0,0 +1,5 @@
public class Building {
public void paint() {
}
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,4 @@
public class SubBuilding extends Building {
}