一、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的更多相关文章

  1. java1.8 新特性(五 如何使用filter,limit ,skip ,distinct map flatmap ,collect 操作 java集合)

    使用filter 根据 条件筛选 出结果:例如 找出 user 中 age >=15 的用户 package lambda.stream; /** * @author 作者:cb * @vers ...

  2. RxJava【过滤】操作符 filter distinct throttle take skip first MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. Spark 学习笔记之 map/flatMap/filter/mapPartitions/mapPartitionsWithIndex/sample

    map/flatMap/filter/mapPartitions/mapPartitionsWithIndex/sample:

  4. RxJava【变换】操作符 map flatMap concatMap buffer MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. Python【day 14】sorted函数、filter函数和map函数的区别

    sorted函数.filter函数和map函数的区别1.作用 前者用于排序, 中者用于筛选, 后者用于返回值(不是特定的筛选或者排序)2.写法 前者 sorted(iterable,key=自定义函数 ...

  6. SELECT DISTINCT ON expressions must match initial ORDER BY expressions

    开发说pg中执行sql报错,发来消息让帮看看: SELECT DISTINCT ON expressions must match initial ORDER BY expressions 详细语句如 ...

  7. Java集合Stream类filter的使用

    之前的Java集合中removeIf的使用一文写了使用removeIf来实现按条件对集合进行过滤.这篇文章使用同样是JDK1.8新加入的Stream中filter方法来实现同样的效果.并且在实际项目中 ...

  8. JavaScript 中 map、foreach、reduce 间的区别

    一直对map.foreach.reduce这些函数很是生疏,今天看underscorejs时好好研究了一下,一研究我就更懵了,这不是一样嘛,都是遍历,所以我就去知乎找了一下,整理出了比较好的几个说法. ...

  9. hadoop 多表join:Map side join及Reduce side join范例

    最近在准备抽取数据的工作.有一个id集合200多M,要从另一个500GB的数据集合中抽取出所有id集合中包含的数据集.id数据集合中每一个行就是一个id的字符串(Reduce side join要在每 ...

随机推荐

  1. PDF转图片,在线PDF转JPG/PNG

    [在线DEMO](https://oktools.net/pdf2img) 原理 使用pdf.js预览图片,pdf.js将pdf通过canvas将每一页渲染出来,然后我们通过canvas的toData ...

  2. Echarts-主题切换

    从网上搜索了相关的方法,是主题之前的切换,但是用的是下拉框类型的,也可以设置div样式,参考官网那种 设置一个div,通过三个图片的点击效果实现切换主题的功能 我用的jQuery和Echarts是cd ...

  3. git ssh 绑定 GitLab

    入职新公司之后,需要使用GitLab,可是我不会啊,又不想麻烦运维大佬,所以自己找乐一下,发现网上都是些很陈旧的教程,所以准备自己记录下来 第一步 设置Git端上的用户名和用户邮箱: 假如入你已经安装 ...

  4. centos7 php-fpm 开机启动

    拷贝php-fpm脚本至/etc/init.d目录(文件在php解压目录) cp /usr/local/src/php-/sapi/fpm/init.d.php-fpm /etc/init.d/php ...

  5. ARM工作模式寻址

    用户模式(User)                 usr 快速中断模式(FIQ) fiq 普通终端模式(IRQ)     irq 保护模式(Supervisor) svc 数据访问终止模式(Abo ...

  6. Ubuntu16.04下caffe CPU版的详细安装步骤

    一.caffe简介 Caffe,是一个兼具表达性.速度和思维模块化的深度学习框架. 由伯克利人工智能研究小组和伯克利视觉和学习中心开发. 虽然其内核是用C++编写的,但Caffe有Python和Mat ...

  7. docker安装配置mongodb

    1 执行 docker search mongo 命令: 2 运行mongo docker run --name mongo -v /mnt/mongodb:/data/db -p 27017:270 ...

  8. Python核心技术与实战——十五|深入了解迭代器和生成器

    我们在前面应该写过类似的代码 for i in [1,2,3,4,5]: print(i) for in 语句看起来很直观,很便于理解,比起C++或Java早起的 ; i<n;i++) prin ...

  9. MySQL中添加、修改、删除约束

    https://blog.csdn.net/dz77dz/article/details/82119000 主要包含的约束: 非空.唯一.check.not null.默认值.主键.外键

  10. Jmeter性能测试--自己看到的博客收集

    性能测试的场景:https://www.cnblogs.com/little-little-bai/p/10338156.html