共计 3251 个字符,预计需要花费 9 分钟才能阅读完成。
两个办法的背景
这两个办法看起来做着同样的事件,但实际上又有些不一样。看源码局部是这样的
package java.util.stream;
map() 办法
/**
* @param <R> The element type of the new stream
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
* <a href="package-summary.html#Statelessness">stateless</a>
* function to apply to each element
* @return the new stream
*/
<R> Stream<R> map(Function<? super T, ? extends R> mapper);
flatMap() 办法
/**
* @param <R> The element type of the new stream
* @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
* <a href="package-summary.html#Statelessness">stateless</a>
* function to apply to each element which produces a stream
* of new values
* @return the new stream
*/
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
Stream map() Method
看源码做揣测,map 是一种两头操作,返回的是 Stream
代码测试
map() 办法
public static void main(String[] args) {System.out.println("Output with simple list");
List<String> vowels = Arrays.asList("A", "E", "I", "O", "U");
vowels.stream().map(vowel -> vowel.toLowerCase())
.forEach(value -> System.out.println(value));
List<String> haiList = new ArrayList<>();
haiList.add("hello");
haiList.add("hai");
haiList.add("hehe");
haiList.add("hi");
System.out.println("Output with nested List of List<String>");
List<String> welcomeList = new ArrayList<>();
welcomeList.add("You got it");
welcomeList.add("Don't mention it");
welcomeList.add("No worries.");
welcomeList.add("Not a problem");
List<List<String>> nestedList = Arrays.asList(haiList, welcomeList);
nestedList.stream().map(list -> {return list.stream().map(value -> value.toUpperCase());
}).forEach(value -> System.out.println(value));
}
Output
Output with simple list
a
e
i
o
u
Output with nested List of List<String>
java.util.stream.ReferencePipeline$3@3b9a45b3
java.util.stream.ReferencePipeline$3@7699a589
flatMap() 办法
public static void main(String[] args) {List<String> haiList = new ArrayList<>();
haiList.add("hello");
haiList.add("hai");
haiList.add("hehe");
haiList.add("hi");
System.out.println("Output with nested List of List<String>");
List<String> welcomeList = new ArrayList<>();
welcomeList.add("You got it");
welcomeList.add("Don't mention it");
welcomeList.add("No worries.");
welcomeList.add("Not a problem");
List<List<String>> nestedList = Arrays.asList(haiList, welcomeList);
nestedList.stream().flatMap(list -> list.stream())
.map(value -> value.toUpperCase())
.forEach(value -> System.out.println(value));
}
Output
Output with nested List of List<String>
HELLO
HAI
HEHE
HI
YOU GOT IT
DON'T MENTION IT
NO WORRIES.
NOT A PROBLEM
Java 8 map() vs flatMap()
- map()和 flatMap()办法都能够利用于 Stream <T> 和 Optional <T>。并且都返回 Stream <R> 或 Optional <U>。
- 区别在于,映射操作为每个输出值生成一个输入值,而 flatMap 操作为每个输出值生成任意数量(零个或多个)的值。在 flatMap()中,每个输出始终是一个汇合,能够是 List 或 Set 或 Map。映射操作采纳一个函数,该函数将为输出流中的每个值调用,并生成一个后果值,该后果值将发送到输入流。flatMap 操作采纳的性能在概念上想耗费一个值并产生任意数量的值。然而,在 Java 中,办法返回任意数量的值很麻烦,因为办法只能返回零或一个值。
代码
public static void main(String[] args) {List<Stream> together = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4)) // Stream of List<Integer>
.map(List::stream)
.collect(Collectors.toList());
System.out.println("Output with map() ->"+together);
List<Integer> togetherFlatMap = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4)) // Stream of List<Integer>
.flatMap(List::stream)
.map(integer -> integer + 1)
.collect(Collectors.toList());
System.out.println("Output with flatMap() ->"+togetherFlatMap);
}
Output
Output with map() -> [java.util.stream.ReferencePipeline$Head@16b98e56, java.util.stream.ReferencePipeline$Head@7ef20235]
Output with flatMap() -> [2, 3, 4, 5]
正文完