1// generic methods
2
3public <T> List<T> fromArrayToList(T[] a) {
4 return Arrays.stream(a).collect(Collectors.toList());
5 }
6
7public static <T, G> List<G> fromArrayToList(T[] a, Function<T, G> mapperFunction) {
8 return Arrays.stream(a)
9 .map(mapperFunction)
10 .collect(Collectors.toList());
11 }
12
13// bounded generics
14
15public <T extends Number> List<T> fromArrayToList(T[] a) {
16 ...
17 }
18
19//multiple bounds
20
21<T extends Number & Comparable>
22
23// upper bound wildcards
24
25public static void paintAllBuildings(List<? extends Building> buildings) {
26 ...
27 }
28
29// lower bound wildcard
30
31<? super T>
1public static <T> T getRandomValue(List<T> listOfPossibleOutcomes, int numPossibilities) {
2 int r = RandomNumberGenerator.getRandIntBetween(0, numPossibilities);
3 return listOfPossibleOutcomes.get(r);
4}
5
6public static <T> T getRandomValueFromGenericList(List<T> list) {
7 Collections.shuffle(list);
8 return list.get(0);
9}