Stream map

转自:https://www.cnblogs.com/ajing2018/p/14281612.html

Stream管道流map的基础用法: (对管道流中的每一个数据元素进行转换操作)
eg:将集合中的每一个字符串,全部转换成大写
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]

posted @ 2022-11-24 15:05  sensen~||^_^|||&  阅读(71)  评论(0)    收藏  举报