Stream之filter、distinct、skip、map、flatMap、match、find、reduce
一、Stream之filter、distinct、skip:
package com.cy.java8; import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors; public class StreamFilter { public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 7, 8); //取出偶数
List<Integer> result = list.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
System.out.println(result); //去重
List<Integer> result1 = list.stream().distinct().collect(Collectors.toList());
System.out.println(result1); //跳过前面的5个元素
List<Integer> result2 = list.stream().skip(5).collect(Collectors.toList());
System.out.println(result2); //只要前面的5个
List<Integer> result3 = list.stream().limit(5).collect(Collectors.toList());
System.out.println(result3);
}
}
打印结果:
[2, 4, 6, 6, 8]
[1, 2, 3, 4, 5, 6, 7, 8]
[6, 6, 7, 7, 8]
[1, 2, 3, 4, 5]
二、Stream之map、flatMap:
package com.cy.java8; import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream; public class StreamMap { public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 6, 7, 7, 8); //list集合中每个数放大两倍
List<Integer> result1 = list.stream().map(i -> i * 2).collect(Collectors.toList());
System.out.println(result1); //只返回Dish中的name
List<String> result2 = listDish().stream().map(d -> d.getName()).collect(Collectors.toList());
System.out.println(result2); /**
* 需求:将Hello和World单词中重复的字母去掉
* flatmap flat(扁平化)
*/
String[] words = {"Hello", "World"};
//{H,e,l,l,o}, {W,o,r,l,d}
Stream<String[]> stream = Arrays.stream(words).map(w -> w.split("")); //Stream<String[]>
//H,e,l,l,o,W,o,r,l,d
Stream<String> stringStream = stream.flatMap(Arrays::stream);
List<String> result3 = stringStream.distinct().collect(Collectors.toList());
System.out.println(result3);
} private static List<Dish> listDish(){
List<Dish> menu = Arrays.asList(
new Dish("pork", false, 800, Dish.Type.MEAT),
new Dish("beef", false, 700, Dish.Type.MEAT),
new Dish("chicken", false, 400, Dish.Type.MEAT),
new Dish("french fries", true, 530, Dish.Type.OTHER),
new Dish("rice", true, 350, Dish.Type.OTHER),
new Dish("season fruit", true, 120, Dish.Type.OTHER),
new Dish("pizza", true, 550, Dish.Type.OTHER),
new Dish("prawns", false, 300, Dish.Type.FISH),
new Dish("salmon", false, 450, Dish.Type.FISH));
return menu;
}
}
打印结果:
[2, 4, 6, 8, 10, 12, 12, 14, 14, 16]
[pork, beef, chicken, french fries, rice, season fruit, pizza, prawns, salmon]
[H, e, l, o, W, r, d]
三、stream之match、find、reduce:
match:anyMatch、noneMatch、allMatch
package com.cy.java8; import java.util.Arrays;
import java.util.stream.Stream; public class StreamMatch { public static void main(String[] args) {
Stream<Integer> s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); //s中是否所有元素都大于0
boolean b = s.allMatch(i -> i > 0);
System.out.println(b); //true //只要有一个元素大于6就返回true
s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
boolean b2 = s.anyMatch(i -> i>6);
System.out.println(b2); //true //没有一个元素满足小于1
s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
boolean b3 = s.noneMatch(i -> i<0);
System.out.println(b3); //true
}
}
find:
package com.cy.java8; import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream; public class StreamFind { public static void main(String[] args) {
Stream<Integer> s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7}); //过滤出偶数之后,随便返回一个
Optional<Integer> optional1 = s.filter(i -> i % 2 == 0).findAny();
System.out.println(optional1.get()); //过滤出大于100的数,任意返回一个,如果有值正常返回,没值返回-1
s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
Optional<Integer> optional2 = s.filter(i -> i > 100).findAny();
System.out.println(optional2.orElse(-1)); //过滤出偶数,找到第一个,如果存在就打印
s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5, 6, 7});
Optional<Integer> optional3 = s.filter(i -> i % 2 == 0).findFirst();
optional3.ifPresent(System.out::println);
}
}
打印:
2
-1
2
reduce:
package com.cy.java8; import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream; public class StreamReduce { public static void main(String[] args) {
//reduce: 聚合的作用
Stream<Integer> s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5}); //求和,初始值设置为0
Integer result = s.reduce(0, (i, j) -> i + j);
System.out.println(result); //求最大值
s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5});
s.reduce((i, j) -> i > j ? i : j).ifPresent(System.out::println); s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5});
s.reduce(Integer::max).ifPresent(System.out::println); //只对里面的偶数进行相乘,计算结果
s = Arrays.stream(new Integer[]{1, 2, 3, 4, 5});
Integer result2 = s.filter(i -> i % 2 == 0).reduce(1, (i, j) -> i * j);
Optional.of(result2).ifPresent(System.out::println);
}
}
打印:
15
5
5
8
-----
Stream之filter、distinct、skip、map、flatMap、match、find、reduce的更多相关文章
- java1.8 新特性(五 如何使用filter,limit ,skip ,distinct map flatmap ,collect 操作 java集合)
使用filter 根据 条件筛选 出结果:例如 找出 user 中 age >=15 的用户 package lambda.stream; /** * @author 作者:cb * @vers ...
- RxJava【过滤】操作符 filter distinct throttle take skip first MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Spark 学习笔记之 map/flatMap/filter/mapPartitions/mapPartitionsWithIndex/sample
map/flatMap/filter/mapPartitions/mapPartitionsWithIndex/sample:
- RxJava【变换】操作符 map flatMap concatMap buffer MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Python【day 14】sorted函数、filter函数和map函数的区别
sorted函数.filter函数和map函数的区别1.作用 前者用于排序, 中者用于筛选, 后者用于返回值(不是特定的筛选或者排序)2.写法 前者 sorted(iterable,key=自定义函数 ...
- SELECT DISTINCT ON expressions must match initial ORDER BY expressions
开发说pg中执行sql报错,发来消息让帮看看: SELECT DISTINCT ON expressions must match initial ORDER BY expressions 详细语句如 ...
- Java集合Stream类filter的使用
之前的Java集合中removeIf的使用一文写了使用removeIf来实现按条件对集合进行过滤.这篇文章使用同样是JDK1.8新加入的Stream中filter方法来实现同样的效果.并且在实际项目中 ...
- JavaScript 中 map、foreach、reduce 间的区别
一直对map.foreach.reduce这些函数很是生疏,今天看underscorejs时好好研究了一下,一研究我就更懵了,这不是一样嘛,都是遍历,所以我就去知乎找了一下,整理出了比较好的几个说法. ...
- hadoop 多表join:Map side join及Reduce side join范例
最近在准备抽取数据的工作.有一个id集合200多M,要从另一个500GB的数据集合中抽取出所有id集合中包含的数据集.id数据集合中每一个行就是一个id的字符串(Reduce side join要在每 ...
随机推荐
- mysql5.7.26做主主配置
一.首先两台服务器安装好mysql数据库环境 参照linux rpm方式安装mysql5.1 https://www.cnblogs.com/sky-cheng/p/10564604.html 二.在 ...
- Thiago2(TPO AI.ROSTO):集成式AI换脸软件(Autodesk Flame)
如标题一样,Thiago2 是一款集成式AI换脸软件(TPO AI.ROSTO),需要与Autodesk Flame结合使用,从demo来看完成度还是很高的,算是一种完全GUI版的DeepFaceLa ...
- Kendo UI使用教程:CDN服务
[Kendo UI最新试用版下载] Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support for React和 ...
- Python 爬虫插件
#coding:utf-8import sys,urllib2,re,Queuesys.path.append("..") from lib.Http_Class import H ...
- CodeForces-916B-Jamie and Binary Sequence(changed after round)(构造)
链接: https://vjudge.net/problem/CodeForces-916B 题意: Jamie is preparing a Codeforces round. He has got ...
- Eclipse修改背景颜色(豆沙绿)
操作界面默认颜色为白色.对于我们长期使用电脑编程的人来说,白色很刺激我们的眼睛,所以我经常会改变workspace的背景色,使眼睛舒服一些.设置方法如下: 1.打开window->Prefere ...
- 正则获取a标签和a标签中的href地址
/(<\/?a.*?>)/ a标签 /<a\b.*?</a>/ 表式以"<a "(有空格) 开始 以"</a>" ...
- VUEX报错 [vuex] Do not mutate vuex store state outside mutation handlers
数组 错误的写法:let listData= state.playList; // 数组深拷贝,VUEX就报错 正确的写法:let listDate= state.playList.slice(); ...
- 【leetcode】1208. Get Equal Substrings Within Budget
题目如下: You are given two strings s and t of the same length. You want to change s to t. Changing the ...
- postman—随机数和Monitors
postman做重复测试时,随机数就有很大的作用,不用每次都输入 在postman的Params中,输入一个左大括号,会显示三种随机数: 也可以在body中设置 随机数如下: {{$guid}}:添加 ...