Although now JDK has reached 14 [as of 2020-08-15], but JDK8 is still the most widely used version, I wrote some commonly used, mainly the company with Java8 is actually more, and since learning Java, also did not study the version of the problem, this is just necessary to understand, and then rely on actual combat skilled application

1. Lambda expressions

Lambda expressions, also known as functional programming, are the first thing that comes to mind when it comes to jdK8. The function is summed up in one sentence to simplify the writing of anonymous inner classes

You should be familiar with threads

Lambda expression notation

Through contrast, feel convenient a lot of. Here’s an example that’s often used, no parameter at the top, with a parameter at the bottom

First define the method, the implementation method is the same, writing is not the same

2. Functional interfaces

With the @functionalInterface identifier, there is one and only one abstract method that can be implicitly converted to a lambda expression. And what this does is, if you write your interface this way, you can use lambda expressions as well.

Define interface methods

3. Default method of interface

Before Java8, the interface was heavily coupled, and if you changed the methods in the interface, you had to change the methods of all the implementation classes. The so-called influence of the whole body. Java8 offers a new feature to solve this problem. You can define the default method for the default decoration. Main does not need to implement foo even if it implements Interface

Benefits: The default method can add new methods to an interface without breaking the implementation of an existing interface.

You can see that foo() does not compile without overwriting it

4, references,

An easy way to write the first lambda expression is to use a reference. If you don’t find it, check it out. Method references use a pair of colons :: to make language construction more compact and reduce redundant code.

5, date,

The previous Date method had a lot of problems

(1) non-thread safety, want to study why you can find online information, here will not elaborate.

Alibaba Java development manual in chapter 1, section 6 – Concurrent processing is also clearly stated

(2) Poor design, Date Date class more than one package

(3) Time processing trouble, most of the time with SimpleDateFormat class use

(4) No internationalization, no time zone support

At that time, no one thought that Java can develop so well, the original design did not think of so much, so the design is not reasonable, now use a new right

To compensate, the java.time package was added to Java8. This package covers all operations that handle date, time, date/time, time zone, instants, during, and clock. Local simplifies date and time processing without time zone problem. ** ** ** ** ** ** Here are the commonly used apis

Local simplifies date and time processing without time zone problem. Zoned(Time zone) − Dates and times are processed according to the specified time zone

There are many new Java8 features, such as Stream API, Optional class, Nashorn, JavaScript engine, etc. If you want to study the information online, I will sum up so much, if you have any questions please point out in the comments section, I will continue to update some dry products. If it helps you, just click on it

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — 9/3 update line — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — –

A Stream is an IO Stream, so it is difficult to learn

Stream is a new concept introduced in Java8 to process data in collections, which can also be understood as a high-level collection.

Before Java8, working with data in collections was tricky, not because the code was hard to write, but because it was cumbersome. The stream just tells it what to do, and it automatically returns the results to you, and we don’t have to write complicated, error-prone multithreaded code.

Antecedents to review

Today, I happened to see a stream of Java8. The previous two days demand just use, if I had learned in advance stream, the code can be much simpler. The demand is this: the front needed about the value of metro fee value, lazy numerical write to death can run, but followed by mana issues, and if the database corresponding values change, it’s certainly not (although the value of the database will not easily change), but it is a very bad habit, we should put an end to this phenomenon. I then processed the data in the back end and retrieved the corresponding value by subway fare from the database. Returns true if the value exists, false if it does not. It’s not much code either, but it would be nice to have a Stream instead. There is a human nature is inertia, there is a simpler reason not to. Next, take a look at the amazing Stream

The traditional way of programming before JAVA8, if we needed to manipulate a collection of data, as I did above, we would have to use the API provided by the collection to get the elements of the collection through a loop. This way of accessing the data would make the code bloated. You can greatly improve efficiency by using a streaming Stream instead of the usual traversal of collections arrays, lists, and maps

In my opinion, Stream is just like a tool, which can help us analyze and process data. It is extremely easy to use. When the data volume is relatively large, Stream can save us a lot of time, but it is not obvious when the data volume is small.

Write a piece of pseudocode (as I described above)

List< object type > lists = Return List set //Java8 before traversing for (int I = 0, length = lists. Size (); i < length; I ++) {database value = subwayValue.get(I).getValue(); If (integer.valueof (front-end values).equals(database values)){flag = true; break; }} // Return Boolean flag = lists.stream().nonematch (I -> i.getValue().equals(' front-end value '));Copy the code

In addition to this, Stream has a number of useful apis (one for each, mostly on your own)

Screening and slicing

Screen for people older than 20

list.stream().filter(item -> iteam.getAge()>20).forEach(System.out:println);
Copy the code

Filter the first five pieces of data

list.stream().limit(5).forEach(System.out:println);
Copy the code

The sorting

List<Integer> List = arrayList.asList (a bunch of unordered numbers); Stream<Integer> stream = list.stream(); stream.sorted().forEach(System.out:println); Stream ().sorted((e1,e2) -> integer.compare (e1.getage (), e2.getage ())).foreach (system.out :println);Copy the code

Match and find

List.stream ().count() list.stream().count()Copy the code

reduction

List<Integer> List = arrayList.asList (a bunch of numbers); List.stream ().reduce(0,Integer::sum) // Calculate the sum of object values list < object type > list = method of returning object; List.stream ().map(object type ::getValue).reduce(value type ::sum)Copy the code

collect

// Return a list list < object type > list = return object method; List< object type > listStream = list.stream().filter(e -> LLDB etValue()>20).collect(Collectors. ToList ()); // Return a set List< object type > List = return object method; List< object type > listStream = list.stream().filter(e -> LLDB etValue()>20).collect(Collectors. ToSet ());Copy the code

What matters is not what they have, but what you can use