BAEL-376 - changing method signatures

This commit is contained in:
slavisa-baeldung
2016-12-04 12:13:12 +01:00
parent 7ca7c69098
commit 9d3b8dfb72
4 changed files with 46 additions and 25 deletions

View File

@@ -7,6 +7,6 @@ public class Building {
private static final Logger LOGGER = LoggerFactory.getLogger(Building.class);
public void paint() {
LOGGER.info("Building");
LOGGER.info("Painting Building");
}
}

View File

@@ -1,27 +1,34 @@
package com.baeldung.generics;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class Generics {
// definition of a generic method
public static <T> List<T> fromArrayToList(T[] a) {
return Arrays.stream(a).collect(Collectors.toList());
}
// definition of a generic method
public static <T> List<T> fromArrayToList(T[] a) {
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) {
return Arrays.stream(a).collect(Collectors.toList());
}
// definition of a generic method
public static <T, G> List<G> fromArrayToList(T[] a, List<G> list, Function<T, G> mapperFunction) {
List<T> listWithTypeT = Arrays.stream(a).collect(Collectors.toList());
return listWithTypeT.stream().map(mapperFunction)
.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.forEach(Building::paint);
return true;
}
// example of a generic method that has Number as an upper bound for T
public static <T extends Number> List<T> fromArrayToListWithUpperBound(T[] a) {
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 void paintAllBuildings(List<? extends Building> buildings) {
buildings.forEach(Building::paint);
}
}

View File

@@ -7,6 +7,6 @@ public class House extends Building {
private static final Logger LOGGER = LoggerFactory.getLogger(House.class);
public void paint() {
LOGGER.info("House");
LOGGER.info("Painting House");
}
}