Skip to content

Commit 5925abc

Browse files
BAEL-376 - simplifying
1 parent b4f05d4 commit 5925abc

1 file changed

Lines changed: 4 additions & 7 deletions

File tree

‎core-java/src/main/java/com/baeldung/generics/Generics.java‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,24 @@
33
import java.util.ArrayList;
44
import java.util.Arrays;
55
import java.util.List;
6+
import java.util.stream.Collectors;
67

78
public class Generics {
89

910
// definition of a generic method
1011
public static <T> List<T> fromArrayToList(T[] a) {
11-
List<T> list = new ArrayList<>();
12-
Arrays.stream(a).forEach(list::add);
13-
return list;
12+
return Arrays.stream(a).collect(Collectors.toList());
1413
}
1514

1615
// example of a generic method that has Number as an upper bound for T
1716
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
18-
List<T> list = new ArrayList<>();
19-
Arrays.stream(a).forEach(list::add);
20-
return list;
17+
return Arrays.stream(a).collect(Collectors.toList());
2118
}
2219

2320
// example of a generic method with a wild card, this method can be used
2421
// with a list of any subtype of Building
2522
public static boolean paintAllBuildings(List<? extends Building> buildings) {
26-
buildings.stream().forEach(Building::paint);
23+
buildings.forEach(Building::paint);
2724
return true;
2825
}
2926

0 commit comments

Comments
 (0)