Article Directory
- First, what is Java Stream API?
- Second, how to obtain Stream flow?
- 2.1, Collection pipeline flow is converted to
- 2.2, Map pipeline flow is converted to
- 3.3, is converted to an array of flow pipes
- 3.4, the text file into a pipeline flow
- Third, the common method
- 3.0, all the code
- 3.1, one by one process: forEash
- 3.2, Filter: filter
- 3.3 mapping: map
- 3.4, sorting: sort
- 3.5 Conversion: collect
- 3.6, the number of statistics: count
- 3.7 interception: limit
- 3.8, jump: skip
- 3.9, a combination of stitching: concat
- Fourth, recommended links
First, what is Java Stream API?
1.1 Introduction
Stream function Java programming interfaces in Java. 8 is initially introduced, and together into the landmark features of Java development and lambda, which greatly facilitates the collection efficiency of the open type data processing art.
Java Stream is a data flow through the pipeline, and manipulation of the data in the pipeline, a pipe and then flows into the next.
Function pipeline comprising: Filter (filter), the Map (mapping), Sort (sorting) or the like, then through a set of Java Stream data processing pipeline, or converted into another group set data output.
Second, how to obtain Stream flow?
(Complete code below, placed inside a class)
package com.zicheng.stream;
import java.util.*;
import java.util.stream.Stream;
/**
* 子诚
* Description:集合、map、数组 获取流
* 时间:2020/4/1 16:44
*/
public class Demo_GetStream {
public static void main(String[] args) {
//list获取Stream流
List<String> list = new ArrayList<>();
Stream<String> listStream = list.stream();
//set获取Stream流
Set<String> set = new HashSet<>();
Stream<String> setStream = set.stream();
//获取Map的Stream流
Map<String, String> map = new HashMap<>();
//获取map中 key 的Stream流
Stream<String> keyStream = map.keySet().stream();
//获取map中 value 的Stream流
Stream<String> valueStream = map.values().stream();
//获取 entry 的Stream流
Stream<Map.Entry<String, String>> entryStream = map.entrySet().stream();
//数组获取Stream流
String[] array = {"河南省", "新乡市", "河南师范大学"};
Stream<String> arrayStream = Stream.of(array);
}
}
2.1, Collection pipeline flow is converted to
NOTE:
java.util.Collection added to the default interface method used to obtain the flow stream, so that all classes can be realized acquisition stream.
Key Code:
list.stream (); set.stream ();
2.2, Map pipeline flow is converted to
Note:
sub-interface java.util.Map interface is not Collection, and is characterized in that a single data structure does not conform KV stream elements, it is necessary to obtain a corresponding stream, respectively; where points that require key, value or entry.
Key Code:
map.keySet () Stream ();.
Map.values () Stream ();.
EnumMap.entrySet () Stream ();.
3.3, is converted to an array of flow pipes
NOTE:
If you are not set (Collection) or map (Map) array but, since the array object can not add the default method, the Stream interface of static methods
Key Code:
Stream.of (Array);
3.4, the text file into a pipeline flow
Files.lines method by converting a text file for the pipeline flow, the figure Paths.get () method is to obtain the effect of the file, the API is a Java NIO!
Stream<String> lines = Files.lines(Paths.get("file.txt"));
Third, the common method
3.0, all the code
package com.zicheng.stream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
/**
* 子诚
* Description:Stream中的常用方法
* 时间:2020/4/1 18:27
*/
public class StreamFunctionDemo {
public static void main(String[] args) {
StreamFunctionDemo demo = new StreamFunctionDemo();
//方法1:逐一处理:forEach
demo.test_forEach();
//方法2:过滤:filter
demo.test_filter();
//方法3:映射:map
demo.test_map();
//方法4:排序:sorted
demo.test_sorted();
//方法6:统计个数test_count()
demo.test_count();
//方法7:测试截取:limit
demo.test_limit();
//方法8:跳过:skip
demo.test_skip();
//方法9:组合:concat
demo.test_concat();
}
//方法1:逐一处理:forEach
void test_forEach() {
String[] array = {"河南省", "新乡市", "河南师范大学"};
Stream<String> arrayStream = Stream.of(array);
/**
* parallel()函数表示对管道中的元素进行并行处理而不是串行处理。
* 但是这样就有可能导致管道流中后面的元素先处理,前面的元素后处理,也就是元素的顺序无法保证。
* 数据量大的话,就能观察到数据顺序是无法保证的.
* 不想用,不用
*/
arrayStream.parallel().forEach(item -> System.out.println(item));
arrayStream.forEach(item -> {
//在这个地方可以对每个item进行操作
System.out.println(item);
});
}
//方法2:过滤:filter
void test_filter() {
// 获取以“河南”开头的数据项
List<String> nameStrs = Arrays.asList("河南省", "新乡市", "河南师范大学");
List<String> list = nameStrs.stream()
.filter(s -> s.startsWith("河南"))
// 转换成了List的数据结构,方便接收和打印
.collect(toList());
System.out.println(list);
}
//方法3:映射:map;将集合中的每一个字符串全部大写
void test_map() {
// Arrays.asList(),将数组转换为字符串
List<String> players = Arrays.asList("Kobe Bean Bryant", "James", "Harden", "Curry", "Durant");
List<String> list = players.stream()
//s-> s.toUpperCase(),可以替换为String::toUpperCase(方法引用)
.map(String::toUpperCase)
// 转换成了List的数据结构,方便接收和打印
.collect(toList());
System.out.println(list);
}
//方法4:排序:sorted
void test_sorted() {
List<Integer> nums = Arrays.asList(2, 6, 5, 4, 8, 7, 9);
//sorted,默认是升序
List<Integer> list = nums.stream().sorted().collect(toList());
System.out.println(list);
//如果想要降序的话
Collections.reverse(list);
System.out.println(list);
}
//方法5:转换:collect
void test_collect() {
//list,set可以互转。
//list转成map的话,index当做key,元素就当做value
//具体,有时间再写
}
//方法6:统计个数:count
void test_count() {
List<String> school = Arrays.asList("河南省", "新乡市", "河南师范大学");
long count = school.stream()
.filter(s -> s.startsWith("河南"))
.count();
//打印个数
System.out.println(count);
}
//方法7:截取:limit
void test_limit() {
List<String> school = Arrays.asList("河南省", "新乡市", "河南师范大学");
List<String> limit = school.stream()
.limit(2)
.collect(toList());
//打印结果
System.out.println(limit);
}
//方法8:跳过:skip
void test_skip() {
List<String> school = Arrays.asList("河南省", "新乡市", "河南师范大学");
List<String> skip = school.stream()
//跳过前两个
.skip(2)
.collect(toList());
//打印结果
System.out.println(skip);
}
//方法9:组合:concat
void test_concat() {
List<String> country = Arrays.asList("地球", "中华人民共和国");
List<String> school = Arrays.asList("河南省", "新乡市", "河南师范大学");
//将上面的两个集合拼接
List<String> address = Stream.concat(country.stream(), school.stream())
.collect(toList());
//打印结果
System.out.println(address);
}
}
3.1, one by one process: forEash
Note:
Although the name and operation, and for all like, but the principle is not the same.
A flow pipe is, one traversal.
//方法1:逐一处理
void test_forEach() {
String[] array = {"河南省", "新乡市", "河南师范大学"};
Stream<String> arrayStream = Stream.of(array);
/**
* parallel()函数表示对管道中的元素进行并行处理而不是串行处理。
* 但是这样就有可能导致管道流中后面的元素先处理,前面的元素后处理,也就是元素的顺序无法保证。
* 数据量大的话,就能观察到数据顺序是无法保证的.
* 不想用,不用
*/
arrayStream.parallel().forEach(item -> System.out.println(item));
arrayStream.forEach(item -> {
//在这个地方可以对每个item进行操作
System.out.println(item);
});
}
3.2, Filter: filter
Note:
by the filter method, a stream converted to another subset of stream
//方法2:过滤
void testFilter() {
List<String> nameStrs = Arrays.asList("河南省", "新乡市", "河南师范大学");
List<String> list = nameStrs.stream()
.filter(s -> s.startsWith("河南"))
//转换成了List的数据结构,方便接受和打印
.collect(toList());
System.out.println(list);
}
3.3 mapping: map
Note:
If you need to map the flow element to another stream, map method can be used.
//方法3:映射:map;将集合中的每一个字符串全部大写
void test_map() {
// Arrays.asList(),将数组转换为字符串
List<String> players = Arrays.asList("Kobe Bean Bryant", "James", "Harden", "Curry", "Durant");
List<String> list = players.stream()
//s-> s.toUpperCase(),可以替换为String::toUpperCase(方法引用)
.map(String::toUpperCase)
// 转换成了List的数据结构,方便接收和打印
.collect(toList());
System.out.println(list);
}
3.4, sorting: sort
//方法4:排序:sorted
void test_sorted() {
List<Integer> nums = Arrays.asList(2, 6, 5, 4, 8, 7, 9);
//sorted,默认是升序
List<Integer> list = nums.stream().sorted().collect(toList());
System.out.println(list);
//如果想要降序的话
Collections.reverse(list);
System.out.println(list);
}
3.5 Conversion: collect
Commonly used to already mentioned above.
//方法5:转换:collect
void test_collect() {
//list,set可以互转。
//list转成map的话,index当做key,元素就当做value
//具体,有时间再写
}
3.6, the number of statistics: count
//方法6:统计个数:count
void test_count() {
List<String> nameStrs = Arrays.asList("河南省", "新乡市", "河南师范大学");
long count = nameStrs.stream()
.filter(s -> s.startsWith("河南"))
.count();
//打印个数
System.out.println(count);
}
3.7 interception: limit
//方法7:测试截取:limit
void test_limit(){
List<String> school = Arrays.asList("河南省", "新乡市", "河南师范大学");
List<String> limit= school.stream()
// 截取前两个
.limit(2)
.collect(toList());
//打印结果
System.out.println(limit);
}
3.8, jump: skip
//方法8:跳过:skip
void test_skip(){
List<String> school = Arrays.asList("河南省", "新乡市", "河南师范大学");
List<String> skip = school.stream()
//跳过前两个
.skip(2)
.collect(toList());
//打印结果
System.out.println(skip);
}
3.9, a combination of stitching: concat
//方法9:组合:concat
void test_concat() {
List<String> country = Arrays.asList("地球", "中华人民共和国");
List<String> school = Arrays.asList("河南省", "新乡市", "河南师范大学");
//将上面的两个集合拼接
List<String> address = Stream.concat(country.stream(), school.stream())
.collect(toList());
//打印结果
System.out.println(address);
}
Fourth, recommended links
Suave Rego : https: //ke.qq.com/teacher/316518226 (Tencent classroom, speak well, recommended a wave)
coquettish protracted brother : https: //ke.qq.com/teacher/1579106394 ( Tencent classroom, funny flavored, knowledgeable)