赞
踩
map函数的作用就是针对管道流中的每一个数据元素进行转换操作。
主要有三种用法
将集合中的每个字符串,全部转化成大写!
不使用流
List<String> list = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur";
//不使用Stream管道流
List<String> listUpper = new ArrayList<>();
for (String s : list) {
listUpper.add(s.toUpperCase());
}
System.out.println(listUpper); //[MONKEY, LION, GIRAFFE, LEMUR]
使用流
List<String> list = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur";
// 使用Stream管道流
List<String> collect = list.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()函数不仅可以处理数据,还可以转换数据的类型
List<String> list = Arrays.asList("Monkey", "Lion", "Giraffe", "Lemur";
List<Integer> lengths = list.stream()
.map(String::length)
.collect(Collectors.toList());
System.out.println(lengths); //[6, 4, 7, 5]
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。
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函数。
map可以对管道流中的数据进行转换操作,但是如果管道中还有管道该如何处理?即:如何处理二维数组及二维集合类。实现一个简单的需求:将“hello”,“world”两个字符串组成的集合,元素的每一个字母打印出来。如果不用Stream我们怎么写?写2层for循环,第一层遍历字符串,并且将字符串拆分成char数组,第二层for循环遍历char数组。
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);
输出打印结果:
java.util.stream.ReferencePipeline$Head@3551a94
java.util.stream.ReferencePipeline$Head@531be3c5
用map方法是做不到的,这个需求用map方法无法实现。map只能针对一维数组进行操作,数组里面还有数组,管道里面还有管道,它是处理不了每一个元素的。
flatMap可以理解为将若干个子管道中的数据全都,平面展开到父管道中进行处理。
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
参考链接:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。