BAEL-376 - simplifying

This commit is contained in:
slavisa-baeldung 2016-12-04 09:59:30 +01:00
parent b4f05d4f6f
commit 5925abc243
1 changed files with 4 additions and 7 deletions

View File

@ -3,27 +3,24 @@ package com.baeldung.generics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Generics {
// definition of a generic method
public static <T> List<T> fromArrayToList(T[] a) {
List<T> list = new ArrayList<>();
Arrays.stream(a).forEach(list::add);
return list;
return Arrays.stream(a).collect(Collectors.toList());
}
// 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<>();
Arrays.stream(a).forEach(list::add);
return list;
return Arrays.stream(a).collect(Collectors.toList());
}
// 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) {
buildings.stream().forEach(Building::paint);
buildings.forEach(Building::paint);
return true;
}