Java8中map()方法和flatMap()方法的使用

发布时间:2020-10-26 14:10:19 作者:Leah
来源:亿速云 阅读:208

Java8中map()方法和flatMap()方法的使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

两个方法的背景

这两个方法看起来做着同样的事情,但实际上又有些不一样。看源码部分是这样的

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<&#63; super T, &#63; 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<&#63; super T, &#63; extends Stream<&#63; 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()

代码

 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()方法的使用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. Java8中Stream的排序,查找和匹配的用法
  2. Java8实用技能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java8 map flatmap

上一篇:利用PHP数据结构实现二分搜索树

下一篇:使用map实现Nginx允许多个域名跨域

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》