Stateless intermediate operation
Java8 practice examples of new features Stream
Intermediate stateless operation, may be processed in a single individual data. For example: When an element filter (filtering), it may be determined, for example map (map) ...
Filtering filter
of map
flat clip of flatMap
pass Li peek
Filtration filter
Receiving a predicate assertion, boolean Pridicate <T> to determine whether a property that the filter according to the type of the return value.
// 过滤 filter
@Test
public void filterTest() {
list.stream()
//过滤掉所有小于2000的商品
.filter(sku -> sku.getSkuPrice() > 2000)
.forEach(item ->
System.out.println(
JSON.toJSONString(
item.getSkuName()+item.getSkuPrice(),
true)));
/**
* 输出的结果
* "无人机4999.0"
* "VR一体机2299.0"
* "跑步机2699.0"
*/
Map map
Stream map support method, receiving a Function <T, R> Interface function type, this method will be applied each element, and maps them into a new stream. In fact, in the above example it has been used very much, for example, receives a SKu type of flow, but after later map, the return of String streams.
//映射 map
@Test
public void mapTest() {
list.stream()
//用方法引用取出来sku中的所有的商品名称
.map(Sku::getSkuName)
.forEach(item -> System.out.println(item));
/**
* 无人机
* VR一体机
* 纯色衬衫
* 牛仔裤
* 跑步机
* Java编程思想
* Java核心技术
* 算法
* TensorFlow进阶指南
*/
}
Flat flatMap
flatMap method is to let each of the values you have a stream flows into another, all the streams are connected into a side stream: Cases
//扁夹化 flatMap
@Test
public void flatMap() {
List<String> words = Arrays.asList("Java 8", "Lambdas", "In", "Action");
List<String[]> listStringArrays = words.stream()
//分割每一个单词
.map(word -> word.split(""))
//对所有的元素去重
.distinct()
//收集成list集合
.collect(Collectors.toList());
listStringArrays.forEach(arr ->
System.out.println(Arrays.toString(arr)));
/**
* 非扁平化
* 这是把每一个数组当成了一整个元素,
* 然后对一整个数组进行去重,
* 如果数组没有重复的就不会去重
* [J, a, v, a, , 8]
* [L, a, m, b, d, a, s]
* [I, n]
* [A, c, t, i, o, n]
*/
List<String> listString = words.stream()
//映射成String[]数组流
.map(word -> word.split(""))
//扁平化
.flatMap(Arrays::stream)
//去重
.distinct()
//排序
.sorted()
//收集
.collect(Collectors.toList());
listString.forEach(System.out::println);
/**
* 扁平化是把所有的数组map映射出来的数组流
* 转换成一个流,而不是一个数组流,这样就可以去重了
* 8 A I J L a b c d i m n o s t v
*/
}
Li peek times
And terminal operation, are circulated. Peek with the method, which receives a comsumer method. This method can best embody intermediate stateless operation:
//遍厉 peek
@Test
public void peekTest() {
list.stream()
//获取商品名称
.map(Sku::getSkuName)
//遍厉
.peek(skuName -> System.out.println(skuName + " peek"))
.forEach(System.out::println);
/**
* 看结果是和forEach效替进行输出,而不是peek输出之后再进行forEach
* 说明,peek不用在所有的元素的基础上进行操作,只用消费就行了
*
* 无人机 peek
* 无人机
* VR一体机 peek
* VR一体机
* 纯色衬衫 peek
* 纯色衬衫
* 牛仔裤 peek
* 牛仔裤
* 跑步机 peek
* 跑步机
* Java编程思想 peek
* Java编程思想
* Java核心技术 peek
* Java核心技术
* 算法 peek
* 算法
* TensorFlow进阶指南 peek
* TensorFlow进阶指南
*/
list.stream()
//获取商品名称
.map(Sku::getSkuName)
//遍厉
.peek(skuName -> System.out.println(skuName + " peek"))
//排序
.sorted()
.forEach(System.out::println);
/**
* 这个地方就是加了一个有状态中间操作
* 因为排序必须在所有的数据基础上进行操作的
*
* 无人机 peek
* VR一体机 peek
* 纯色衬衫 peek
* 牛仔裤 peek
* 跑步机 peek
* Java编程思想 peek
* Java核心技术 peek
* 算法 peek
* TensorFlow进阶指南 peek
* Java核心技术
* Java编程思想
* TensorFlow进阶指南
* VR一体机
* 无人机
* 牛仔裤
* 算法
* 纯色衬衫
* 跑步机
*/
}
Personal humble opinion, if not, ask righting!