package com.java8test.lambda;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* ClassName:TestStream <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2017-11-18 23:41 <br/>
*
* @author xuwen
* @see
* @since JDK 1.8
*/
public class TestStream {
public static void main(String[] args) {
List<Employee> employeeList = Arrays.asList(
new Employee("张三", 58, 9999.99,"男"),
new Employee("赵四", 58, 100,"女"),
new Employee("李四", 38, 5555.99,"男"),
new Employee("王五", 18, 6666.66,"男"),
new Employee("赵六", 86, 3333.33,"女"),
new Employee("赵六", 16, 3333.33,"男"),
new Employee("赵八", 8, 3333.33,"女"),
new Employee("田七", 8, 7777.77,"男")
);
String[] names = {"chaimm", "peter", "john"};
//List获取stream
Stream<Employee> employeeStream = employeeList.stream();
//数组获取stream
Stream<String> stringStream = Arrays.stream(names);
//直接将值变成流对象
Stream<String> stringStream1 = Stream.of("chaimm", "peter", "john");
//筛选
System.out.println("=========筛选==========");
List<Employee> fitlerEmployee = employeeList.stream().filter(employee -> employee.getAge() > 50).collect(Collectors.toList());
fitlerEmployee.forEach(System.out::println);
//去除重复
System.out.println("=========去除重复==========");
List<String> distinctEmployee = employeeList.stream().map(Employee::getName).distinct().collect(Collectors.toList());
distinctEmployee.forEach(System.out::println);
//截取
System.out.println("=========截取==========");
List<Employee> limitEmployee = employeeList.stream().limit(2).collect(Collectors.toList());
limitEmployee.forEach(System.out::println);
//跳过
System.out.println("=========跳过==========");
List<Employee> skipEmployee = employeeList.stream().skip(3).collect(Collectors.toList());
skipEmployee.forEach(System.out::println);
//映射
System.out.println("=========映射==========");
List<String> employeeNameList = employeeList.stream().map(Employee::getName).collect(Collectors.toList());
employeeNameList.forEach(System.out::println);
//合并多个流 (例:列出List中各不相同的单词)
System.out.println("=========合并多个流==========");
List<String> list = new ArrayList<String>();
list.add("I am a boy");
list.add("I love the girl");
list.add("But the girl loves another girl");
List<String> unionString = list.stream()
.map(line -> line.split(" "))
.flatMap(Arrays::stream)
.distinct()
.collect(Collectors.toList());
unionString.forEach(System.out::println);
//是否匹配任一元素:anyMatch
System.out.println("=========是否匹配任一元素:anyMatch==========");
System.out.println(employeeList.stream().anyMatch(employee -> employee.getAge() > 60));
//是否匹配所有元素
System.out.println("=========是否匹配所有元素==========");
System.out.println(employeeList.stream().allMatch(employee -> employee.getAge() > 60));
//是否未匹配所有元素
System.out.println("=========是否未匹配所有元素==========");
System.out.println(employeeList.stream().noneMatch(employee -> employee.getAge() < 10));
//获取任一元素findAny(findAny能够从流中随便选一个元素出来,它返回一个Optional类型的元素)
System.out.println("=========获取任一元素findAny==========");
Optional<Employee> employeeOptional = employeeList.stream().findAny();
//获取第一个元素findFirst
System.out.println("=========获取第一个元素findFirst==========");
employeeOptional = employeeList.stream().findFirst();
//元素求和:自定义Lambda表达式实现求和
System.out.println("=========元素求和:自定义Lambda表达式实现求和==========");
System.out.println(employeeList.stream().map(Employee::getAge).reduce(0,(x,y) -> x+y).intValue());
Optional<Integer> integerOptional = employeeList.stream().map(Employee::getAge).reduce((x,y)->x+y);
System.out.println(integerOptional.get());
//元素求和:使用Integer.sum函数求和
System.out.println("=========元素求和:使用Integer.sum函数求和==========");
int countAge = employeeList.stream().map(Employee::getAge).reduce(0, Integer::sum);
System.out.println("countAge="+countAge);
//将普通流转换成数值流
System.out.println("=========将普通流转换成数值流==========");
IntStream intStream = employeeList.stream().mapToInt(Employee::getAge);
intStream = employeeList.stream().mapToInt(x -> x.getAge());
//数值计算
System.out.println("=========数值计算==========");
System.out.println(employeeList.stream().mapToInt(Employee::getAge).max().getAsInt());
System.out.println(employeeList.stream().mapToInt(Employee::getAge).max().isPresent());
System.out.println("=========计数==========");
System.out.println(employeeList.stream().collect(Collectors.counting()));
System.out.println(employeeList.stream().count());
System.out.println("=========最值==========");
Optional<Employee> maxEmployeeOptional = employeeList.stream().collect(Collectors.maxBy(Comparator.comparingInt(Employee::getAge)));
System.out.println(maxEmployeeOptional.get());
Optional<Employee> minEmployeeOptional = employeeList.stream().collect(Collectors.minBy(Comparator.comparingInt(Employee::getAge)));
System.out.println(minEmployeeOptional.get());
System.out.println("=========求和==========");
Long sumAge = employeeList.stream().collect(Collectors.summingInt(Employee::getAge)).longValue();
System.out.println(sumAge);
System.out.println("=========平均值==========");
Double avgAge = employeeList.stream().collect(Collectors.averagingInt(Employee::getAge));
System.out.println(avgAge);
System.out.println("=========一次性计算所有归约操作==========");
IntSummaryStatistics intSummaryStatistics = employeeList.stream().collect(Collectors.summarizingInt(Employee::getAge));
System.out.println("年龄平均值:"+intSummaryStatistics.getAverage());
System.out.println("年龄总个数:"+intSummaryStatistics.getCount());
System.out.println("年龄总和:"+intSummaryStatistics.getSum());
System.out.println("最大年龄:"+intSummaryStatistics.getMax());
System.out.println("最小年龄:"+intSummaryStatistics.getMin());
System.out.println("toString:"+intSummaryStatistics.toString());
System.out.println("=========连接字符串==========");
String allName = employeeList.stream().map(Employee::getName).collect(Collectors.joining());
System.out.println(allName);
allName = employeeList.stream().map(Employee::getName).collect(Collectors.joining(", "));
System.out.println(allName);
System.out.println("=========一般性的归约操作==========");
Integer sumEmployeeAge = employeeList.stream().collect(Collectors.reducing(0, Employee::getAge, (x,y) -> x+y));
System.out.println(sumEmployeeAge);
System.out.println("=========一般性的归约操作==========");
Optional<Integer> sumEmployeeAgeOptional = employeeList.stream().map(Employee::getAge).collect(Collectors.reducing((x,y)
没有合适的资源?快使用搜索试试~ 我知道了~
JAVA8新特性Lambda表达式、Stream使用
共30个文件
class:13个
java:10个
xml:6个
4星 · 超过85%的资源 需积分: 47 17 下载量 184 浏览量
2017-11-19
15:10:39
上传
评论
收藏 42KB ZIP 举报
温馨提示
需要环境:JDK8 下载下来后 直接导入IDE工具中,运行TestLambda、TestLambda2、TestLambda3、PracticeLambda、TestStream中的main方法就可以看见结果
资源推荐
资源详情
资源评论
收起资源包目录
java8Test.zip (30个子文件)
java8Test
src
com
java8test
lambda
TestLambda2.java 3KB
TestLambda.java 4KB
PracticeLambda.java 3KB
TestStream.java 12KB
TestLambda3.java 3KB
inter
MyPredicate.java 365B
PracticeInterface1.java 344B
PracticeInterface2.java 341B
PracticeInterface3.java 339B
Employee.java 1KB
.idea
misc.xml 273B
uiDesigner.xml 9KB
workspace.xml 42KB
encodings.xml 159B
inspectionProfiles
Project_Default.xml 552B
modules.xml 258B
java8Test.iml 423B
out
production
java8Test
com
java8test
lambda
TestLambda$2.class 1KB
TestLambda3.class 7KB
Employee.class 2KB
TestStream.class 14KB
TestLambda2.class 3KB
TestLambda.class 6KB
PracticeLambda.class 6KB
TestLambda$1.class 1002B
TestLambda2$1.class 688B
inter
PracticeInterface2.class 278B
PracticeInterface1.class 280B
MyPredicate.class 337B
PracticeInterface3.class 401B
共 30 条
- 1
资源评论
- IT__zhao2018-05-13下载下载下载
java2xw
- 粉丝: 7
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于微信小程序的通用订票系统设计源码
- 基于ThinkPHP的轻量级PHP开发框架设计源码
- 基于CSS的响应式鲜花网站全屏效果设计源码
- 基于JavaScript的访客预约系统设计源码
- 基于Vue和ECharts的工作租房数据可视化系统设计源码
- 1040g0cg310ravpiu6ibg5pg00tsipsln3ju2d0g 2
- 基于Python的SAR图像去噪CNN-NLM设计源码
- redhat6升级到redhat7,过程redhat6.x-> redhat6.10->rehat7.9 主版本最高版本
- 基于Django的流程引擎设计源码
- 基于Node.js的Express框架与MySQL的后台管理系统设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功