Stream allows us to process data declaratively.
brief introduction
- In Java, collection and array are two common data structures
- Similar to the form of SQL statements querying data from a database, Stream provides a high level of abstraction for Java collection operations and representations.
- The set of elements to be processed is treated as a stream and transmitted in the pipeline. These elements, such as filtering, sorting and aggregation, can be processed at each node of the pipeline.
Characteristic
- No space. Stream is just a view of data source, which can be expressed as array, container or I/O channel.
- Stream the data source without changing it. For example, filtering a stream does not delete the filtered elements, but generates a new stream that does not contain the filtered elements
- Lazy load. Operations on streams are performed only when they are needed.
- No repeat consumption. Elements can only be accessed once during the life of a flow.
operation
Creation of flow
-
Set up creation
List<String> list = Arrays.asList("Hello", "Word", "!"); Stream<String> stream = list.stream();
-
Stream's own method
Stream<String> streams = Stream.of("Hello", "Word", "!");
intermediate operation
- Multiple intermediate operations combine to form a pipeline, and the intermediate operation returns a new Stream.
-
filter
System.out.println(streams.stream().filter((e) -> "Hello".equals(e))); strings.stream().filter(e -> !e.isEmpty()).forEach(System.out::println);
-
mapping
- map(Function f): receives a function as an argument, which is applied to each element in the stream and mapped to a new element.
-
Flatmap (function > mapper): receive a function as a parameter, change each value in the flow into another flow, and then connect all flows into one flow
List<String> numbers = Arrays.asList("a","b"); numbers.stream().map(i -> i + i).forEach(System.out::println); Stream<List<String>> stream2 = Stream.of(Arrays.asList("H","E"), Arrays.asList("L", "L", "O")); stream2.flatMap(list -> list.stream()).forEach(System.out::println);
-
limit/skip
- limit returns the first N elements in the Stream. skip discards the first N elements in the Stream.
List<String> numbers = Arrays.asList("a","b"); numbers.stream().limit(1).forEach(System.out::println); numbers.stream().skip(1).forEach(System.out::println);
-
sort
- sorted(): natural sorting uses compatible's int compareTo(T o) method
- sorted(Comparator com): custom sort using Comparator's int compare(T o1, T o2) method
List<String> numbers = Arrays.asList("b","a"); numbers.stream().sorted().forEach(System.out::println); numbers.stream().sorted((x,y) -> y.compareTo(x)).forEach(System.out::println);
-
distinct
- Duplicate removal
List<String> numbers = Arrays.asList("b","a","b"); numbers.stream().distinct().forEach(System.out::println);
-
reduce
- accumulation
System.out.println("=====reduce:Combine the elements in the flow repeatedly to get a value=========="); Stream<Integer> stream = Stream.iterate(1, x -> x+1).limit(200); //stream.forEach(System.out::println); Integer sum = stream.reduce(10,(x,y)-> x+y); System.out.println(sum);
terminal operation
- If the return value is not Stream, it is terminal operation (immediate evaluation). Terminal operation does not support chain call, which will trigger actual calculation.
- After the end operation, the Stream cannot be used again, and no intermediate operation is allowed.
-
forEach
- ergodic
List<String> numbers = Arrays.asList("b","a","b"); numbers.stream().forEach(System.out::println);
-
count
- Statistical number
List<String> numbers = Arrays.asList("b","a"); numbers.stream().count();
-
collect
- Combination, return the corresponding types, including list, set, TreeSet, map, etc.
List<String> numbers = Arrays.asList("b","a"); numbers = numbers.stream().sorted().collect(Collectors.toList()); List<lambdaDemo> lambdaDemos = Arrays.asList(new lambdaDemo("zhang","bing",1), new lambdaDemo("li","hua",2)); Map<Integer,String> maps = lambdaDemos.stream().collect(Collectors.toMap(lambdaDemo::getAge,lambdaDemo::getName)); System.out.println(maps); //Age is the only value. public static Map<Integer,lambdaDemo> check(List<lambdaDemo> lambdaDemos){ return lambdaDemos.stream().collect(Collectors.toMap(lambdaDemo::getAge, Function.identity(), (existing, replacement) -> existing)); }
-
Grouping and partitioning
- Collectors.groupingBy() group s elements.
- Collectors.partitioningBy() performs a two partition operation on the element.
public static void test9() { List<lambdaDemo> lambdaDemos = Arrays.asList(new lambdaDemo("zhang","Black",65), new lambdaDemo("li","Black",40), new lambdaDemo("liu","Whites",40)); System.out.println("=======Group by skin color=========================="); Map<String, List<lambdaDemo>> map = lambdaDemos.stream().collect(Collectors.groupingBy(lambdaDemo::getName)); System.out.println(map); System.out.println("=======Multi level grouping according to the age range of people=========================="); Map<Integer, Map<String, List<lambdaDemo>>> map2 = lambdaDemos.stream().collect(Collectors.groupingBy(lambdaDemo::getAge, Collectors.groupingBy( ( p ) -> { if ( p.getAge() > 60 ) { return "Aged"; } else { return "Young people"; } } ) )); System.out.println(map2); } public static void test10() { List<lambdaDemo> lambdaDemos = Arrays.asList(new lambdaDemo("zhang","Black",60), new lambdaDemo("li","Black",2), new lambdaDemo("liu","Whites",3)); System.out.println("========According to whether the person's age is greater than 40========================"); Map<Boolean, List<lambdaDemo>> map = lambdaDemos.stream().collect(Collectors.partitioningBy(p -> p.getAge() > 40)); System.out.println(map); }
github blog list address
github
Welcome to the public account for more details: