好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java基础理论Stream管道流Map操作示例

一、回顾Stream管道流map的基础用法

最简单的需求:将集合中的每一个字符串,全部转换成大写!

?

1

2

3

4

5

6

7

8

9

10

11

12

List<String> alpha = Arrays.asList( "Monkey" , "Lion" , "Giraffe" , "Lemur" );

//不使用Stream管道流

List<String> alphaUpper = new ArrayList<>();

for (String s : alpha) {

     alphaUpper.add(s.toUpperCase());

}

System.out.println(alphaUpper); //[MONKEY, LION, GIRAFFE, LEMUR]

// 使用Stream管道流

List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());

//上面使用了方法引用,和下面的lambda表达式语法效果是一样的

//List<String> collect = alpha.stream().map(s -> s.toUpperCase()).collect(Collectors.toList());

System.out.println(collect); //[MONKEY, LION, GIRAFFE, LEMUR]

所以map函数的作用就是针对管道流中的每一个数据元素进行转换操作。

二、处理非字符串类型集合元素

map()函数不仅可以处理数据,还可以转换数据的类型。如下:

?

1

2

3

4

List<Integer> lengths = alpha.stream()

         .map(String::length)

         .collect(Collectors.toList());

System.out.println(lengths); //[6, 4, 7, 5]

?

1

2

3

Stream.of( "Monkey" , "Lion" , "Giraffe" , "Lemur" )

         .mapToInt(String::length)

         .forEach(System.out::println);

输出如下:

6
4
7
5

除了mapToInt。还有maoToLong,mapToDouble等等用法

三、再复杂一点:处理对象数据格式转换

还是使用上一节中的Employee类,创建10个对象。需求如下:

将每一个Employee的年龄增加一岁 将性别中的[M]换成[male],F换成Female。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

public static void main(String[] args){

     Employee e1 = new Employee( 1 , 23 , "M" , "Rick" , "Beethovan" );

     Employee e2 = new Employee( 2 , 13 , "F" , "Martina" , "Hengis" );

     Employee e3 = new Employee( 3 , 43 , "M" , "Ricky" , "Martin" );

     Employee e4 = new Employee( 4 , 26 , "M" , "Jon" , "Lowman" );

     Employee e5 = new Employee( 5 , 19 , "F" , "Cristine" , "Maria" );

     Employee e6 = new Employee( 6 , 15 , "M" , "David" , "Feezor" );

     Employee e7 = new Employee( 7 , 68 , "F" , "Melissa" , "Roy" );

     Employee e8 = new Employee( 8 , 79 , "M" , "Alex" , "Gussin" );

     Employee e9 = new Employee( 9 , 15 , "F" , "Neetu" , "Singh" );

     Employee e10 = new Employee( 10 , 45 , "M" , "Naveen" , "Jain" );

     List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);

     /*List<Employee> maped = employees.stream()

             .map(e -> {

                 e.setAge(e.getAge() + 1);

                 e.setGender(e.getGender().equals("M")?"male":"female");

                 return e;

             }).collect(Collectors.toList());*/

     List<Employee> maped = employees.stream()

             .peek(e -> {

                 e.setAge(e.getAge() + 1 );

                 e.setGender(e.getGender().equals( "M" )? "male" : "female" );

             }).collect(Collectors.toList());

     System.out.println(maped);

}

由于map的参数e就是返回值,所以可以用peek函数。peek函数是一种特殊的map函数,当函数没有返回值或者参数就是返回值的时候可以使用peek函数。

四、flatMap

map可以对管道流中的数据进行转换操作,但是如果管道中还有管道该如何处理?即:如何处理二维数组及二维集合类。实现一个简单的需求:将[hello],[world]两个字符串组成的集合,元素的每一个字母打印出来。如果不用Stream我们怎么写?写2层for循环,第一层遍历字符串,并且将字符串拆分成char数组,第二层for循环遍历char数组。

?

1

2

3

4

List<String> words = Arrays.asList( "hello" , "word" );

words.stream()

         .map(w -> Arrays.stream(w.split( "" )))    //[[h,e,l,l,o],[w,o,r,l,d]]

         .forEach(System.out::println);

输出打印结果:

?

1

2

java.util.stream.ReferencePipeline$Head @3551a94

java.util.stream.ReferencePipeline$Head @531be3c5

用map方法是做不到的,这个需求用map方法无法实现。map只能针对一维数组进行操作,数组里面还有数组,管道里面还有管道,它是处理不了每一个元素的。

flatMap可以理解为将若干个子管道中的数据全都,平面展开到父管道中进行处理。

?

1

2

3

words.stream()

         .flatMap(w -> Arrays.stream(w.split( "" ))) // [h,e,l,l,o,w,o,r,l,d]

         .forEach(System.out::println);

输出打印结果:

h
e
l
l
o
w
o
r
d

以上就是java基础理论Stream管道流Map操作示例的详细内容,更多关于java Stream管道流Map操作的资料请关注其它相关文章!

原文链接:https://zimug.blog.csdn.net/article/details/106896553

查看更多关于java基础理论Stream管道流Map操作示例的详细内容...

  阅读:24次