两个方法的背景
这两个方法看起来做着同样的事情,但实际上又有些不一样。看源码部分是这样的
package java.util.stream;
map()方法
/** * @paramThe element type of the new stream * @param mapper a non-interfering, * stateless * function to apply to each element * @return the new stream */ Stream map(Function<? super T, ? extends R> mapper);
flatMap()方法
/** * @paramThe element type of the new stream * @param mapper a non-interfering, * stateless * function to apply to each element which produces a stream * of new values * @return the new stream */ Stream 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"); Listvowels = Arrays.asList("A", "E", "I", "O", "U"); vowels.stream().map(vowel -> vowel.toLowerCase()) .forEach(value -> System.out.println(value)); List haiList = new ArrayList<>(); haiList.add("hello"); haiList.add("hai"); haiList.add("hehe"); haiList.add("hi"); System.out.println("Output with nested List of List "); List welcomeList = new ArrayList<>(); welcomeList.add("You got it"); welcomeList.add("Don't mention it"); welcomeList.add("No worries."); welcomeList.add("Not a problem"); List > 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 Listjava.util.stream.ReferencePipeline$3@3b9a45b3 java.util.stream.ReferencePipeline$3@7699a589
flatMap()方法
public static void main(String[] args) { ListhaiList = new ArrayList<>(); haiList.add("hello"); haiList.add("hai"); haiList.add("hehe"); haiList.add("hi"); System.out.println("Output with nested List of List "); List welcomeList = new ArrayList<>(); welcomeList.add("You got it"); welcomeList.add("Don't mention it"); welcomeList.add("No worries."); welcomeList.add("Not a problem"); List > 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 ListHELLO HAI HEHE HI YOU GOT IT DON'T MENTION IT NO WORRIES. NOT A PROBLEM
Java 8 map() vs flatMap()
代码
public static void main(String[] args) { Listtogether = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4)) // Stream of List .map(List::stream) .collect(Collectors.toList()); System.out.println("Output with map() -> "+together); List togetherFlatMap = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4)) // Stream of List .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]
总结
到此这篇关于关于Java8中map()和flatMap()的文章就介绍到这了,更多相关Java8中map()和flatMap()内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!