- stream流的生成
```
// 1,校验通过Collection 系列集合提供的stream()或者paralleStream()
ArrayList list = new ArrayList();
Stream stream = list.stream();
// 2.通过Arrays的静态方法stream()获取数组流
String[] strs = new String[10];
Stream<String> stream2 = Arrays.stream(strs);
// 3.通过Stream类中的静态方法of
Stream<String> stream3 = Stream.of("a", "b", "c");
// 4.创建无限流
/ 迭代
Stream<Integer> stream4 = Stream.iterate(0, (x) -> x + 1);
// 生成
Stream<Double> stream5 = Stream.generate(()-> Math.random());
```
- Stream的中间操作:
```
1. stream流的筛选过滤去重
Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小军", 3),new Employee("小李", 2));
stream.filter(x -> x.getEmployId() > 1)
.limit(2)
.skip(1)
// 需要流中的元素重写hashCode和equals方法
.distinct()
.forEach(System.out::println);
---------------------------------------------------------------------------------------
2. 生成新的流,通过map映射
Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小军", 3),new Employee("小李", 2));
stream.map(e-> e.getEmployeeName())
.forEach(System.out::println);
---------------------------------------------------------------------------------------
3. stream流自然排序 定制排序
Stream<Employee> stream = Stream.of(new Employee("小王", 1),new Employee("小军", 3),new Employee("小李", 2));
stream.sorted((emp1, emp2) -> {
if(emp1.getEmployId() > emp2.getEmployId()) {
return -1;
}else if(emp1.getEmployId() == emp2.getEmployId()) {
return 0;
}else {
return 1;
}
})
.forEach(System.out::println);
```
- Stream的终止操作:
```
/**
* 终止操作
* 查找和匹配
* allMatch-检查是否匹配所有元素
* anyMatch-检查是否至少匹配一个元素
* noneMatch-检查是否没有匹配所有元素
* findFirst-返回第一个元素
* findAny-返回当前流中的任意元素
* count-返回流中元素的总个数
* max-返回流中最大值
* min-返回流中最小值
*/
List<Employee> asList = Arrays.asList(new Employee("wang", 1),new Employee("wang", 2),new Employee("wang", 3));
// 注意,当集合元素为0时返回true
boolean allMatch = asList.stream().allMatch(emp-> emp.getEmployId() >= 1);
System.out.println(allMatch);
boolean anyMatch = asList.stream().anyMatch(emp-> emp.getEmployId() > 2);
System.out.println(anyMatch);
boolean noneMatch = asList.stream().noneMatch(emp-> emp.getEmployId() > 4);
System.out.println(noneMatch);
Optional<Employee> findFirst = asList.stream().findFirst();
System.out.println(findFirst.get());
Optional<Employee> findAny = asList.parallelStream().findAny();
System.out.println(findAny.get());
long count = asList.stream().count();
System.out.println(count);
Optional<Employee> max = asList.stream().max((emp1, emp2)->Integer.compare(emp1.getEmployId(), emp2.getEmployId()));
System.out.println(max.get());
Optional<Employee> min = asList.stream().min((emp1, emp2)->Integer.compare(emp1.getEmployId(), emp2.getEmployId()));
System.out.println(min.get());
---------------------------------------------------------------------------------------------------------------------
// reduce操作: reduce:(T identity,BinaryOperator)/reduce(BinaryOperator)-可以将流中元素反复结合起来,得到一个值
List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
Integer count2 = list.stream()
.reduce(0, Integer::sum);
System.out.println(count2);
Optional<Integer> sum = list.stream()
.reduce(Integer::sum);
System.out.println(sum.get());
---------------------------------------------------------------------------------------------------------------------
// collect操作:Collect-将流转换为其他形式,接收一个Collection接口的实现,用于给Stream中元素做汇总的方法
List<Employee> asList = Arrays.asList(new Employee("wang", 1),new Employee("wang", 2),new Employee("wang", 3));
List<Integer> collect = asList.stream().map(e->e.getEmployId()).collect(Collectors.toList());
System.out.println(collect);
```
*****
原文链接:https://blog.csdn.net/qq_29411737/article/details/80835658
- 2111总结
- 1.面向对象
- 1.0.1 super()与this()的区别
- 1.0.2 private、default、protected、public的访问范围
- 1.0.3 continue、break、return区别
- 1.0.4 重载和重写的区别
- 1.0.5 final的特点
- 1.0.6 抽象类与接口的区别
- 1.0.7 java类型
- 1.0.8 什么是反射
- 1.0.9 类的加载机制
- 1.1.1 jvm内存结构
- 1.1.2 java垃圾回收机制
- 1.1.3 并发问题
- 1.1.3.1 线程的状态与关系
- 1.1.3.2 并发的三大性质
- 1.1.3.3 线程的实现与使用
- 1.1.3.4 线程池相关
- 1.1.3.5 并发相关方法
- 1.1.3.6 线程相关工具
- 1.1.4 jdk8特性
- 1.1.4.1 lambad表达式的使用
- 1.1.4.2 stream API
- 1.1.4.3 Optional容器使用
- 1.1.4.4 LocalDateTime
- 1.15 io流
- 1.16 动态代理实现
- 2.JavaEE
- 2.0.1 JSP四大作用域九大内置对象
- 2.0.2 cookie与session的区别
- 4.数据库相关
- 5.git版本管理
- 7.一些问题解决
- 7.1 分布式锁如何实现