The Java version is now available in JDK13, and companies are still using JDK8. It is important to know some of the new features in JDK8, such as the elegant null-optional class, streams for action sets, and functional programming. Here are some examples of common Stream operations.
Stream flow profile
A sequence of elements supporting sequential and parallel aggregate operations. Stream is A queue of elements from A data source that supports aggregate operations
Stream A classification of common methods in a Stream
- 1. Perform intermediate operations
- 2. Terminate the operation
Common intermediate operations for Stream
- Peek ()– Every element in the consumption flow
List<Integer> list = Arrays.asList(6.5.5.1.4);
list.stream().peek(i -> System.out.println(i)).forEach(i-> System.out.println(i));
Copy the code
- 2. Map () mapToInt() mapToDouble() and so on – maps each element in the stream to another element
int[] nums={1.2.3.5.6};
int sum = IntStream.of(nums).map(i->i*2).sum();
Copy the code
- 3. Filter ()– Filter out the elements in the stream that meet the requirements
List<Integer> list = Arrays.asList(6.5.5.1.4);
list.stream().filter(integer -> integer>1).forEach(System.out::println);
Copy the code
- 4. Sort ()– Sorts the elements in the stream according to certain rules
List<Integer> list = Arrays.asList(6.5.5.1.4);
List<Integer> collect = list.stream().sorted((o1, o2) -> o1 - o2).collect(Collectors.toList());
Copy the code
- 5. Distinct () – to heavy
List<Integer> list = Arrays.asList(6.5.5.1.4);
list.stream().dinstinct().forEach(System.out::println)
Copy the code
- 6. Limit ()– Truncates the stream with a specified number of elements
List<Integer> list = Arrays.asList(6.5.5.1.4);
list.stream().limit(1).forEach(System.out::println);
Copy the code
The common termination operation of Stream
- 1.count()- Gets the number of elements in the stream
List<Integer> list = Arrays.asList(1.2.3.5.6);
long count = list.stream().count();
Copy the code
- 2. Reduce ()- Merges all elements of a stream into one element, which can be used for summing, taking products, and so on
List<Integer> list = Arrays.asList(1.2.3.5.6);
Integer reduce = list.stream().reduce((integer, integer2) -> integer + integer2).get();
Copy the code
- 3. ForEach ()- Traverses all elements in the stream
List<Integer> list = Arrays.asList(1.2.3.5.6);
list.stream().forEach(System.out::println);
Copy the code
- 4. Max ()- Find the maximum number of elements in the stream
List<Integer> list = Arrays.asList(1.2.3.5.6);
Integer max = list.stream().max((s1, s2) -> s1 - s2).get();
Copy the code
- 5. Min ()- Find the minimum value of the element in the stream
List<Integer> list = Arrays.asList(1.2.3.5.6);
Integer min = list.stream().max((o1, o2) -> o2 - o1).get();
Copy the code
- AnyMaych ()/allMatch()/noneMatch()– Matches the value in the element, returning Boolean
List<Integer> list = Arrays.asList(1.2.3.5.6);
boolean b = list.stream().noneMatch((x) -> x.intValue() == 1);
boolean b = list.stream().allMatch((x) -> x.intValue() == 1);
boolean b = list.stream().anyMatch((x) -> x.intValue() == 1);
Copy the code
- 7. Collect ()– Elements in the Collectors flow are handled by the tool class
List<Integer> list = Arrays.asList(1.2.3.5.6);
// Take the average
Double average= list.stream().collect(Collectors.averagingInt(x -> x.intValue()));
/ / for grouping
Double average= list.stream().collect(Collectors.groupinngby(x -> x.intValue()));
// Collect it into a List
List<Integer> collect = list.stream().sorted((o1, o2) -> o1 - o2).collect(Collectors.toList());
Copy the code
- 8. There are many computational methods encapsulated in summerizing, such as summation, taking the average and so on
Double sum_= list.stream().collect(Collectors.summarizingInt(value -> value.intValue())).getAverage();
Copy the code