Here are two methods.test1()
and test2()
public class MoreStreams {
public static void main(String[] args) {
List<String> names = Arrays.asList("Brad", "Kate", "Kim", "Jack",
"Joe", "Mike", "Susan", "George", "Robert", "Julia", "Parker", "Benson");
test1(names);
//test2(names);
}
private static void test1(List<String> names) {
List<String> result = names.stream()
.map(name -> sub(toUpper(toLower(name))))
.collect(Collectors.toList());
System.out.println(result);
}
private static void test2(List<String> names) {
List<String> result = names.stream()
.map(MoreStreams::toLower)
.map(MoreStreams::toUpper)
.map(MoreStreams::sub)
.collect(Collectors.toList());
System.out.println(result);
}
private static String toUpper(String name) {
System.out.println("to Upper: " + name);
return name.toUpperCase();
}
private static String toLower(String name) {
System.out.println("to Lower: " + name);
return name.toLowerCase();
}
private static String sub(String name) {
System.out.println("to Title: " + name);
return name.substring(1);
}
}
The first one uses multiple map()
, and the second one aggregates all logic together in one map()
, do they take the same time or not, and why ?
And what if there are more map()
s in the chain? Please help me.