时间:2021-05-19
两个方法的背景
这两个方法看起来做着同样的事情,但实际上又有些不一样。看源码部分是这样的
package java.util.stream;map()方法
/*** @param <R> The element type of the new stream* @param mapper a <a href="package-summary.html#NonInterference" rel="external nofollow" rel="external nofollow" >non-interfering</a>,* <a href="package-summary.html#Statelessness" rel="external nofollow" rel="external nofollow" >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" rel="external nofollow" rel="external nofollow" >non-interfering</a>,* <a href="package-summary.html#Statelessness" rel="external nofollow" rel="external nofollow" >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 listaeiouOutput with nested List of List<String>java.util.stream.ReferencePipeline$3@3b9a45b3java.util.stream.ReferencePipeline$3@7699a589flatMap()方法
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>HELLOHAIHEHEHIYOU GOT ITDON'T MENTION ITNO WORRIES.NOT A PROBLEMJava 8 map() vs flatMap()
代码
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]总结
到此这篇关于关于Java8中map()和flatMap()的文章就介绍到这了,更多相关Java8中map()和flatMap()内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
简介Map是java中非常常用的一个集合类型,我们通常也需要去遍历Map去获取某些值,java8引入了Stream的概念,那么我们怎么在Map中使用Stream
Java8的flatMap函数,作用是:如果有值,为其执行mapping函数返回Optional类型返回值,否则返回空Optional。见到的映射函数往往都只有
前言在Java8之前,默认情况下,接口中的所有方法都是公共的和抽象的。但是这一限制在Java8中被打破了,Java8允许开发人员在接口中添加新方法,而无需在实现
Java8推出了全新的日期时间API,在教程中我们将通过一些简单的实例来学习如何使用新API。Java处理日期、日历和时间的方式一直为社区所诟病,将java.u
map和flatMap主要分在集合上的使用和在可选类型上的使用,下面分别来看下。集合上使用map和flatMap先看如下的代码:funcgetInfos(byn