当前位置:网站首页>Getting started with lambda8 new features
Getting started with lambda8 new features
2022-06-25 15:24:00 【Running pig ZC】
Lambda8 Getting started with new features
In our daily development process , I can't avoid writing some simple functions , for instance , Multiply each person's age by two to return , Judge whether the age is older than 18 wait , If every time you encounter this kind of problem , If we all write a function , It seems to be a little troublesome , But in java8 in , Encapsulate all these functions , It can be divided into the following four types
| name | describe | Corresponding method | Enter the reference ( type ) | The ginseng ( type ) |
|---|---|---|---|---|
| Function<S,T> | Functional | apply | Y | Y(T) |
| Consumer | Consumer type | accept | Y | N |
| Supplier | Application type | get | N | Y |
| Predicate | Judgmental | test | Y | Y |
Example
Function
Use scenarios : By entering a value , You also need to return the corresponding result value after processing
The type of acceptance is String
The return type is Integer
Function<String, Integer> function = s ->{
return 1;
};
System.out.println (function.apply ("200"));
Consumer
Use scenarios : By entering a certain value , Don't care about the returned results , Most commonly used for Flow computation
The type of acceptance is String
no return value
Consumer<String> consumer = s -> {
System.out.println (s + "22");
};
consumer.accept ("dasdsa");
Supplier
Use scenarios : No participation , But I want to get some kind of return value , It is usually used to obtain a certain Pool resources , For example, get jedis The connection of , Database connection, etc
You do not need to accept parameters
The type of return is String
Supplier<String> supplier = () -> {
return "wew";
};
System.out.println (supplier.get ());
Predicate
Use scenarios : Accept one String Parameters of type , This parameter is logically calculated , Finally, you need to return a 0/1 The logical value of , For example, it is to judge whether a student is older than 14 wait
The accepted type is String
The return type is always bool
Predicate<String> predicate = s -> {
return s.length () > 0;
};
System.out.println (predicate.test ("ewe"));
The above seems a little too simple , Let's have some advanced ones
Give the following code snippet , Print out Age >=18, Gender is ’ Woman ’, The man's id Or name and return a
List<Student> studentList = new ArrayList<>();
studentList.add(new Student(1, " Zhang San ", 10, " male "));
studentList.add(new Student(2, " Li Si ", 7, " male "));
studentList.add(new Student(3, " Xi Shi ", 20, " Woman "));
studentList.add(new Student(4, " The sable cicada ", 18, " Woman "));
Their thinking
loop list Filter out age >= 18 Get a collection list1
loop list1, Filter out gender =‘ Woman ’ Get a collection list2
Loop print out list2
The approximate code is as follows
List<Student> list1 = new ArrayList<>(); for (Student student : studentList){ if (student.getAge() >= 18){ list1.add(student); } } List<Student> list2 = new ArrayList<>(); for (Student student : list1){ if (" Woman ".equals(student.getSex())){ list2.add(student); } } System.out.println(list2.get(0));It seems that there are a lot , And there is a lot of repetitive code , But the overall logic is relatively clear , Nothing wrong , So is there an optimized way to write ???
Optimized version
Here, we consider the adoption of the above jdk Several methods are provided , The specific code is as follows
String studentName = studentList.stream()
.filter(x -> x.getAge() >= 18)
.filter(x -> " Woman ".equals(x.getSex()))
.map(Student::getName)
.collect(Collectors.toList())
.get(0);
System.out.println(studentName);
The code has been modified , It is more regular than the previous version ( grace ) A lot , In the following code, we can often use this kind of streaming programming , It can greatly simplify the readability of our code .
In the above functions , Accept the following parameter types
- filter
public final Stream<P_OUT> filter(Predicate<? super P_OUT> predicate)
- map
public final <R> Stream<R> map(Function<? super P_OUT, ? extends R> mapper)
That's what we're sharing , Welcome to communicate and discuss with us ~~~
边栏推荐
- CV pre training model set
- Stderr and stdout related standard outputs and other C system APIs
- Use Matplotlib to draw a line chart
- How to deal with mining process
- QT excel table read / write library - qtxlsx
- One code per day - day one
- Paddlepaddle paper reproduction course biggan learning experience
- Leetcode121 timing of buying and selling stocks
- The last glory of the late Ming Dynasty - the battle of Korea
- Advertising effect cluster analysis (kmeans)
猜你喜欢

Several common optimization methods
![[paper notes] contextual transformer networks for visual recognition](/img/e4/45185983e28664564bbf79023ccbf6.jpg)
[paper notes] contextual transformer networks for visual recognition

Design and implementation of timer

Js- get the mouse coordinates and follow them

Graphic control and layout basis of R visualization
![[paper notes] mcunetv2: memory efficient patch based influence for tiny deep learning](/img/4b/f446bd37057237c0ba4c7b4e38e74f.jpg)
[paper notes] mcunetv2: memory efficient patch based influence for tiny deep learning

Some usage records about using pyqt5

One question per day,

‘make_ unique’ is not a member of ‘std’

Learning notes on February 5, 2022 (C language)
随机推荐
Yolov4 coco pre train Darknet weight file
QT inline dialog
Judging the number of leap years from 1 to N years
Qt: Pro project file
Image segmentation based on deep learning: network structure design
Go language modifies / removes multiple line breaks in strings
Sampling method and descriptive statistical function in R language
System Verilog - thread
Using Visual Studio
AB string interchange
Real variable instance
Shared memory synchronous encapsulation
网上办理股票开户安全吗?
Function of getinstance() method
p1408
[paper notes] overview of case segmentation
QT database connection deletion
Sequential programming 1
High precision addition
How to deal with mining process