关键词:
简介
jdk1.8中新增了流Stream,和Lambda表达式
Stream流、Lambda表达式是函数式编程思想,可以让我们不用关注什么对象,而是关注对数据进行了什么操作,
Stream可以很方便对我们的集合数组进行操作。并且在大数据下处理效果高,列如有大数据量集合可以使用
我们的并行流进行操作。可以消灭嵌套地狱的if。让代码简洁、易理解,并且、快速开发。
常用操作:
常用操作(中间操作)
1. distinct():去除流中的重复元素
2. filter():对流中的元素进行条件过滤,符合过滤条件的才能继续留在流中
3. map():对流中的元素进行计算或转换
4. sorted():对流中的元素进行排序
5. limit():可以设置流的最大长度,超出的部分将被抛弃。
6. skip():跳过流中的前n个元素,返回剩下的元素
7. flatMap():map只能把一个对象转换成另一个对象来作为流中的元素。而flatMap可以把一个对象转换成多个对象作为流中的元素
9. 常用操作:(终结操作)
10. foreach():对流中的元素进行遍历操作,我们通过传入的参数去指定对遍历到的元素进行什么具体操作。
11. count();获取元素的个数
12. max&min():获取流中最大最小值
13. collect():把当前流转换一个集合
14. anyMatch():判断是否有任意符合匹配条件的元素,结果为boolean
15. allMatch():用来判断是否都符合匹配条件,结果为boolean类型
16. noneMatch():判断流中的元素是否都不符合匹配条件,结果为boolean类型
17. findAny():获取流中的任意一个元素。该方法没有办法保证获取的一定是流中的第一个元素。
18. findFirst():获取流中的第一个元素。
19. parallel():将字符流转为并行流
20. peek():查看线程执行的信息
案列:
/**
* 打印年龄小于18,并且去重
*/
public static void test1(List<Author> authors)
// List<Author> authors1 = new ArrayList<>();
authors.stream()
.distinct()
.filter(author -> author.getAge() < 18)
.forEach(author -> System.out.println("年龄小于18岁的作家为:" + author.getName()));
/**
* 打印名字长度大于1的作家名称
*/
public static void testAuthors()
Stream<Author> stream = getAuthors().stream();
stream.filter(author -> author.getName().length() > 1)
.forEach(author -> System.out.println(author.getName()));
/**
* 去重,并且排序,输出年龄
*/
public static void test5()
List<Author> authors = getAuthors();
// 对流中的元素按照年龄进行降序排序,并且要求不能有重复的元素。
authors.stream()
.distinct()
.sorted((o1, o2) -> o2.getAge() - o1.getAge())
.forEach(author -> System.out.println(author.getAge()));
/**
* 去重,按照年龄排序,输出前两位的作家名称
*/
public static void test6()
List<Author> authors = getAuthors();
authors.stream().distinct()
.sorted()
.limit(2)
.forEach(author -> System.out.println(author.getName()));
/**
* 打印除了年龄最大的作家外的其他作家,要求不能有重复元素,并且按照年龄降序排序。
*/
public static void test7()
List<Author> authors = getAuthors();
authors.stream()
.distinct()
.sorted((o1, o2) -> o2.getAge() - o1.getAge())
.skip(1)
.forEach(author -> System.out.println(author.getName()));
/**
* 打印所有书籍的名字。要求对重复的元素进行去重。
*/
public static void test8()
List<Book> books = new ArrayList<>();
List<Author> authors = getAuthors();
authors.stream()
.flatMap(author -> author.getBooks().stream())
.distinct()
.forEach(book -> books.add(book));
System.out.println(books);
/**
* 使用map去重输出姓名
*/
public static void test9()
List<Author> authors = getAuthors();
authors.stream()
.map(author -> author.getBooks())
.distinct()
.forEach(s -> System.out.println(s));
/**
* 打印所有书籍的名字。要求对重复的书名进行去重。
*/
public static void test10()
List<Author> authors = getAuthors();
authors.stream()
.flatMap(author -> author.getBooks().stream())
.map(book -> book.getName())
.distinct()
.forEach(System.out::println);
/**
* 打印现有数据的所有分类。要求对分类进行去重。不能出现这种格式:哲学,爱情,并获取一个list集合
*/
public static void test11()
List<String> collect = getAuthors().stream()
.flatMap(author -> author.getBooks().stream())
.distinct()
.flatMap(books -> Arrays.stream(books.getCategory().split(",")))
.distinct()
.collect(Collectors.toList());
System.out.println(collect);
/**
* 分别获取这些作家的所出书籍的最高分和最低分并打印。
*/
public static void test12()
List<Author> authors = getAuthors();
Optional<Integer> max = authors.stream()
.flatMap(author -> author.getBooks().stream())
.map(book -> book.getScore())
.max((o1, o2) -> o1 - o2);
Optional<Integer> min = authors.stream()
.flatMap(author -> author.getBooks().stream())
.map(book -> book.getScore())
.min((o1, o2) -> o1 - o2);
System.out.println("最大=" + max.get() + "最小=" + min.get());
/**
* 获取一个存放所有作者名字的List集合。
*/
public static void test13()
List<Author> authors = getAuthors();
List<String> collect = authors.stream()
.map(author -> author.getName())
.distinct()
.collect(Collectors.toList());
System.out.println(collect);
/**
* 获取一个所有书名的Set集合。
*/
public static void test14()
List<Author> authors = getAuthors();
Set<String> collect = authors.stream()
.flatMap(author -> author.getBooks().stream())
.map(book -> book.getName())
.collect(Collectors.toSet());
System.out.println(collect);
/**
* 获取一个Map集合,map的key为作者名,value为List<Book>
*/
public static void test15()
List<Author> authors = getAuthors();
Map<String, List<Book>> collect = authors.stream()
.distinct()
.collect(Collectors.toMap(author -> author.getName(), author -> author.getBooks()));
/**
* 判断是否有年龄在29以上的作家
*/
public static void test16()
List<Author> authors = getAuthors();
boolean b = authors.stream()
.anyMatch(author -> author.getAge() > 22);
System.out.println(b);
/**
* 判断是否所有的作家都是成年人
*/
public static void test17()
List<Author> authors = getAuthors();
boolean b = authors.stream().allMatch(author -> author.getAge() > 11);
System.out.println(b);
/**
* 判断作家是否都没有超过100岁的。
*/
public static void test18()
List<Author> authors = getAuthors();
boolean b = authors.stream()
.noneMatch(author -> author.getAge() > 10);
System.out.println(b);
/**
* 获取任意一个年龄大于18的作家,如果存在就输出他的名字
*/
public static void test19()
List<Author> authors = getAuthors();
Optional<Author> any = authors.stream()
.filter(author -> author.getAge() > 18)
.findAny();
any.ifPresent(author -> System.out.println(author.getName()));
/**
* 获取一个年龄最小的作家,并输出他的姓名。
*/
public static void test20()
List<Author> authors = getAuthors();
Optional<Author> first = authors.stream()
.sorted()
.findFirst();
first.ifPresent(author -> System.out.println(author.getName()));
/**
* 使用reduce求所有作者年龄的和
*/
public static void test21()
List<Author> authors = getAuthors();
Integer reduce = authors.stream()
.distinct()
.map(author -> author.getAge())
.reduce(0, (result, element) -> result + element);
System.out.println(reduce);
/**
* 使用reduce求所有作者中年龄的最大值
*/
public static void test22()
List<Author> authors = getAuthors();
Integer reduce = authors.stream()
.map(author -> author.getAge())
.reduce(Integer.MIN_VALUE, (result, element) -> result < element ? element : result);
System.out.println(reduce);
/**
* 使用reduce求所有作者中年龄的最小值
*/
public static void test23()
List<Author> authors = getAuthors();
Integer reduce = authors.stream()
.map(author -> author.getAge())
.reduce(Integer.MAX_VALUE, (result, element) -> result > element ? element : result);
System.out.println(reduce);
/**
* 使用reduce求所有作者中年龄的最小值,用一个参数的重载方法去求最小值代码如下:
*/
public static void test24()
List<Author> authors = getAuthors();
Optional<Integer> reduce = authors.stream()
.map(author -> author.getAge())
.reduce((result, element) -> result < element ? result : element);
reduce.ifPresent(integer -> System.out.println(integer));
/**
* 打印作家中年龄大于17并且姓名的长度大于1的作家。并且去重
*/
public static void test25()
List<Author> collect = getAuthors().stream()
.filter(new Predicate<Author>()
@Override
public boolean test(Author author)
return author.getAge() > 14;
.and(new Predicate<Author>()
@Override
public boolean test(Author author)
return author.getName().length() > 1查看详情
函数式编程之组合性:函数式编程为什么如此吸引人?
...来自函数式编程。他因何有如此魔力呢?组合行为的高阶函数在函数式编程:接收函数,作为输入或返回一个函数,作为输出这种函数叫高阶函数(High-orderfunction),就如高中数学的复合函数f(g(x))。高... 查看详情
技术干货|java函数式编程之——stream流
在很多人去面试的时候被问起Stream流,如果没有玩过JDK8的Stream人都以为面试官在问你IO流,然后你一说IO流面试官直接否定你,然后再让你说Stream,然后…并且现在很多公司里写的代码也开始大量地加入了Stream流... 查看详情
java之函数式接口
函数式接口概述:接口中只有一个抽象方法下面介绍的可能很抽象,理解不了,至少在我看来单独的这几个借口是没有用的,跟最下面说的Stream流一起用才会有效果函数式接口,即适用于函数式编程场景的接口。而Java中的函数式编程... 查看详情
java8新特性之lambda(代码片段)
...用它们,比如变量的声明。当一个编程语言需要支持高阶函数时,就需要用到lambda表达式。高阶函数是指以其它函数作为参数或者返回函数作为结果的函数。这一节的代码在ch02包中现在,随着在Java8中引进了lambda表达... 查看详情
《java8实战》读书笔记12:函数式编程(代码片段)
...13.4小结第14章函数式编程的技巧14.1无处不在的函数14.1.1高阶函数(higher-orderfunction)14.1.2科里化14.2持久化数据结构14.3Stream的延迟计算14.4模式匹配14.5杂项14.5.1缓存或记忆表14.5.2“返回同样的对象”意味着什么14.5.3结合器1... 查看详情
java8:函数式编程stream
函数式编程:什么是函数式接口?接口中有且只有一个抽象方法。当然默认方法,静态方法,私有方法可以包含。什么是语法糖?更加方便,原理不变的语法。函数式接口的定义:一个接口中只有一个抽象方法即可。例如:@Funct... 查看详情
cmd运行java文件不显示——stream代码简洁之道的详细用法(代码片段)
...处理数据的方式。Java8中之所以费这么大的功夫引入 函数式编程 ,原因有两个:代码简洁函数式编程写出的代码简洁且意图明确,使用stream接口让你从此告别for循环。多核友好,Java函数式编程使得编写并行程序从未如此... 查看详情
java-函数式编程流(stream)
...。反之,迭代的业务执行在集合代码中,称为内部迭代(函数式编程)。语言描述可能有点抽象,下面看实例。1.外部迭代调用itrator方法,产生一个新的Iterator对象,进而控制整个迭代过程。for(Studentstudent:l 查看详情
java8函数式编程实例(代码片段)
什么是函数式编程函数式编程是java8的一大特色,也就是将函数作为一个参数传递给指定方法。别人传的要么是基本数据类型,要么就是地址引用,我们要穿一个“动作”。Stream说到函数式编程,就不得不提及Stream,Stream... 查看详情
java进阶-网络编程socket函数式接口常用的函数式接口
1.网络通信协议 网络通信协议:通信协议是对计算机必须遵守的规则,只有遵守这些规则,计算机之间才能进行通信。这就好比在道路中行驶的汽车一定要遵守交通规则一样,协议中对数据的传输格式、传输速率、传输步骤... 查看详情
java8学习笔记
...am接口而不是for循环。多核友好,parallel()方法。相关知识高阶函数高阶函数就是接收函数参数的函数,能够根据传入的函数参数调节自己的行为。类似C语言中接收函数指针的函数。最经典的就是接收排序比较函数的排序函数。... 查看详情
[三]java8函数式编程stream概念深入理解stream运行原理stream设计思路
Stream的概念定义 官方文档是永远的圣经~ 表格内容来自https://docs.oracle.com/javase/8/docs/api/ Packagejava.util.stream 一节部分原文内容的翻译 intsum=widgets.stream().filter(b->b.getColor()==RED 查看详情
kotlin函数式编程①(函数式编程简介|高阶函数|函数类别|transform变换函数|过滤函数|合并函数|map变换函数|flatmap变换函数)(代码片段)
文章目录一、函数式编程简介1、编程范式2、高阶函数3、函数式编程4、前端开发技术二、函数类别三、变换函数四、map变换函数1、map函数原型分析2、map函数设计理念3、代码示例五、flatMap变换函数1、flatMap函数原型分析2、代码... 查看详情
java8stream,map,option等函数式编程的使用例子(代码片段)
java8stream,map等函数式编程的一些典型用法例子如下。例子1:数组元素字母小写变大写。List<String>list=Arrays.asList("a","b","c","d");List<String>collect=list.stream().map(String::toUpp 查看详情
java8stream,map,option等函数式编程的使用例子(代码片段)
java8stream,map等函数式编程的一些典型用法例子如下。例子1:数组元素字母小写变大写。List<String>list=Arrays.asList("a","b","c","d");List<String>collect=list.stream().map(String::toUpp 查看详情
java8函数式编程:类比sparkrdd算子的stream流操作(代码片段)
1Stream流对集合进行迭代时,可调用其iterator方法,返回一个iterator对象,之后便可以通过该iterator对象遍历集合中的元素,这被称为外部迭代(for循环本身正是封装了其的语法糖),其示意图如下:除此之外,还有内部迭代方法... 查看详情
kotlin小知识之高阶函数(代码片段)
文章目录高阶函数定义高阶函数函数类型高阶函数示例内联函数内联函数的作用内联函数的用法noinline与crossinline高阶函数定义高阶函数高阶函数和Lambda的关系是密不可分的.像接受Lambda参数的函数就可以称为具有函数式编程风格... 查看详情