- 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 分布式鎖如何實現