P
Pxc_春春彭

## 数组声明

``  List<Integer>  list =Arrays.asList(1,4,7,9,3,1);``

## 遍历数组

``````Arrays.asList( "a", "e", "d" ).forEach(e -> System.out.println( e ) );
``````

`````` Arrays.asList( "a", "b", "d" ).forEach( e -> {
System.out.print( e );
System.out.print( e );
} );``````

## 排序

`````` Collections.sort(list, (s1, s2) -> s1.compareTo(s2));
``````

## 过滤filter

``````  List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
``````

## forEach

'forEach' 来迭代流中的每个数据

## map

map 方法用于映射每个元素到对应的结果

``````List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
// 获取对应的平方数
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());``````

## limit

limit 方法用于获取指定数量的流

``````Random random = new Random();
random.ints().limit(10).forEach(System.out::println);``````

## sorted

sorted 方法用于对流进行排序

``````Random random = new Random();
random.ints().limit(10).sorted().forEach(System.out::println);``````

## Collectors

Collectors 类实现了很多归约操作，例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串

``````List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

System.out.println("筛选列表: " + filtered);
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString);``````

## IntSummaryStatistics(求最大、最小、总量、计数)

``````List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);

IntSummaryStatistics stats = integers.stream().mapToInt((x) -> x).summaryStatistics();

System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());``````

P

### Pxc_春春彭

Java8 新特性之流式数据处理

2018/10/10
0
0
Java8 - 使用Stream API

Java8 - 使用Stream API Stream 是什么？ Stream represents a sequence of objects from a source, which supports aggregate operations（多种操作）. Following are the characteristics......

2018/09/12
0
0
Java8新特性系列（引用）

2018/02/03
0
0
java8之Lambda知识回顾

DevSiven
2018/01/05
0
0
java8新特性介绍

yjpfj1203
2016/12/15
17
0

package com.atguigu.util;import java.sql.Connection;import java.sql.SQLException;import java.util.Properties;import javax.sql.DataSource;import com.alibaba.druid......

architect刘源源

43
0
P30 Pro劲敌！DxO官宣新机：排行榜又要变

5月26日晚间，DxOMark官方推特预告，将在5月27日公布一款新机型的DxOMark评分，猜猜是哪款？ 网友猜想的机型有：红米K20、谷歌Pixel 3a、索尼Xperia 1、诺基亚9 PureView等。 DxOMark即将公布...

linux-tao

15
0
Ubuntu18.04.2窗口过小不能自适应（二次转载）

tahiti_aa

2
0

6
0
Nginx提供下载apk服务

Jack088

4
0