欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关于Java8中map()和flatMap()的一些事

程序员文章站 2022-07-06 15:53:41
两个方法的背景这两个方法看起来做着同样的事情,但实际上又有些不一样。看源码部分是这样的package java.util.stream;map()方法/*** @param the...

两个方法的背景

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

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 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]

总结

到此这篇关于关于java8中map()和flatmap()的文章就介绍到这了,更多相关java8中map()和flatmap()内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: java8 map flatmap