<>Java8 New features

Java 8 API Added a new abstraction called flow Stream, Allows you to process data in a declarative way .
Stream Use a similar SQL Statement provides an intuitive way to query data from a database Java Higher order abstraction of set operation and expression .
Stream API Can be greatly improved Java Programmer productivity , Let programmers write efficiently , clean , Simple code .
This style treats the set of elements to be processed as a stream , Flow in pipe , And it can be processed on the nodes of the pipeline , Like screening , sort , Polymerization, etc .
Element flow is intermediate in the pipeline (intermediate operation) Treatment of , Final operation (terminal operation) Get the results of the previous processing .

<> catalog

* Why use Stream
* Instance data source
* method
* filter
* map
* sorted
* limit
* forEach
* collect
<> Why use Stream

* The benefits of functional programming are particularly obvious . This code more expresses the intention of business logic , Rather than its implementation mechanism . Easy to read code is also easy to maintain , More reliable , Less prone to error .
* high-end ,
## Instance data source static ArrayList<people> peoples = new ArrayList<>(); static { for (
int i = 0; i < 100; i++) { peoples.add(new people("sjy" + i, i + 10)); peoples.
add(new people("sjy1" + i, i + 10)); } } static class people { String name; int
age; public int getAge() { return age; } public people(String name, int age) {
this.name = name; this.age = age; } @Override public String toString() { return
"people{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
<> method

Stream Interfaces provide many ways , The following are often used : iteration , Conditional filtering , Convert flow to set and aggregate elements , Get a specified number of streams, etc

filter() API The given explanation is : Returns a flow consisting of elements of this flow that match this given predicate . Method is used to filter out elements through the set conditions , It's actually conditional filtering

condition : obtain age Greater than or equal to 100 User information for
//filter It's a condition , Parameter usage Lambda expression List<people> collect = peoples.stream().filter(
people-> people.getAge() >= 100).collect(Collectors.toList()); System.out.
println(collect.size()); // The output is 20

map Method is used to map each element to the corresponding result , You can understand it by looking at the code
condition : Get all ages , Get all users
// You can see from the code getAge() The return value of the method is int type , therefore List<Integer> List<Integer> collect = peoples.
stream().map(people -> { return people.getAge(); }).collect(Collectors.toList())
; System.out.println(collect); // You can see from the code toString() The return value of the method is String type , therefore List<String>
List<String> collect1 = peoples.stream().map(people::toString).collect(
Collectors.toList()); System.out.println(collect1);
sorted Method for sorting by convection
Actually use peoples.stream().forEach(System.out::println) The order of traversal is different from that of adding ,
stream() − Create a serial stream for a collection .
parallelStream() − Create a parallel flow for a collection .
condition : Sort by age
System.out.println(peoples); List<people> collect = peoples.stream().sorted(
Comparator.comparingInt(people::getAge)).collect(Collectors.toList()); System.
out.println(collect); List<people> collect2 = peoples.stream().sorted((o1, o2) -
> o2.getAge()-o1.getAge()).collect(Collectors.toList()); System.out.println(
limit Method to get a specified number of flows
List<people> collect = peoples.stream().limit(3).collect(Collectors.toList());
System.out.println(collect.size());// Output results 3
peoples.stream().forEach(people -> System.out.println(people.toString()));

You can see that all the methods are followed by this collect method . The condition of this method is Collector class ,
Collectors Class implements many reduction operations , For example, converting streams to collections and aggregate elements .Collectors Can be used to return a list or string :
It's actually execution map The stream of retrieved elements is converted to the corresponding data format , as Set List Map etc.
Set<Integer> collect = peoples.stream().map(people::getAge).collect(Collectors
.toSet()); System.out.println(collect.size()); List<Integer> collect1 = peoples.
stream().map(people::getAge).collect(Collectors.toList()); System.out.println(
Add a grouping method

condition : Group according to the same age

You know, yes list When a generic attribute is grouped, only one line of code cannot be completed , Just write Java1.7 The previous method , Direct use Java1.8 New features in , Is it really fragrant ·······
Map<Integer, List<people>> collect = peoples.stream().collect(Collectors.
groupingBy(people::getAge)); System.out.println(collect.size());
If used MyBatis-plus We know , This heel MyBatis The constructors in are very similar , We add a series of conditions directly , Get the results you want .
public class Stream_Demo { static ArrayList<people> peoples = new ArrayList<>()
; static { for (int i = 0; i < 100; i++) { peoples.add(new people("sjy" + i, i +
10)); peoples.add(new people("sjy1" + i, i + 10)); } } static class people {
String name; int age; public int getAge() { return age; } public people(String
name, int age) { this.name = name; this.age = age; } @Override public String
toString() { return "people{" + "name='" + name + '\'' + ", age=" + age + '}'; }
} public static void main(String[] args){ //filter It's a condition , Parameter usage Lambda expression
/*List<people> collect = peoples.stream().filter(people -> people.getAge() >=
100).collect(Collectors.toList()); System.out.println(collect.size());*/
/*List<Integer> collect = peoples.stream().map(people -> { return
people.getAge(); }).collect(Collectors.toList()); System.out.println(collect);
List<String> collect1 =
System.out.println(collect1);*/ /*System.out.println(peoples); List<people>
collect =
System.out.println(collect); List<people> collect2 =
peoples.stream().sorted((o1, o2) ->
System.out.println(collect2);*/ /*List<people> collect =
peoples.stream().limit(3).collect(Collectors.toList()); //
System.out.println(collect.size()); long end,start;
peoples.stream().forEach(people::toString);*/ /*Set<Integer> collect =
System.out.println(collect.size()); List<Integer> collect1 =
System.out.println(collect1.size());*/ Map<Integer, List<people>> collect =
peoples.stream().collect(Collectors.groupingBy(people::getAge)); System.out.
println(collect.size()); } }

©2019-2020 Toolsou All rights reserved,
JAVA Detailed explanation of anomalies MySQL An interview is a must ! How to use it quickly html and css Write static page R Language cluster analysis case Dialogue between apple and Nissan suspended ,Apple Car How's it going ?java Realize the function of grabbing red packets SpringBoot practice ( five ):mybatis-plus In BaseMapper,Iservice and ServiceImpl Google says home office affects work efficiency !2021 Return to offline office in 2010 about keras use fit_generator Encountered in StopIteration Programmer Tanabata Valentine's Day confession code