Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic knowledge of java8?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the basic knowledge of java8? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

I. basic knowledge

1. Passing code through behavior parameterization

A) Java8 used to be mainly reflected in the policy pattern (java.io.FilenameFilter). In the past, we usually had to pass an anonymous object or instantiate a concrete class to achieve this.

B) Java8 can pass a method as a parameter, which was not available in previous versions, which is called a first-class function and achieves the right of value (first-class citizen)

2. Lambda expression

It realizes the way of passing the code to the method, can have less code, can realize decoupling and code reuse, and looks very organized, and provides a parallel and custom collector, and it is very convenient to carry out data statistics.

1. Anonymous / function / delivery / brevity

two。 Functional interface (@ functionalinterface) / function descriptor (the signature of an abstract method of a functional interface is basically the signature of a Lambda expression, which we call a function descriptor, for example, the Runnable interface can be seen as a function signature that accepts nothing and returns nothing).

3. Surround the execution mode (try (BufferedReader br=new BufferedReader (new FileReader ("data.txt") {}, so that only one line can be read. If two lines, it needs to be encapsulated into a processFile ((BufferedReader br)-> br.readLine () + br.readLine ())-> so you need to customize a function interface and throw an exception (return p.process (br)).

4. Several new functional interfaces:

1.Predicate test () method-- > T-> Boolean

Accept () method of 2.Consume-> T-> ()

Get () method of 3.Supplier-> ()-> T

4.Function 's apply () method-"convert T to R"

5. UnaryOperator-> extends Function (represents an operator, which can be used for calculation)

6.BiFunction/BiPredicate/BinaryOperate (Extends BiFunction) (Bi means to accept two functions and return 1)

5. Generic (pseudo-generic) + (generic array) + (generic erasure (Object/ boundary) + frequent unpacking (IntPredicate/LongPredicate (generic specialization))

6. Any functional interface is not allowed to throw checked exceptions (exceptions are divided into Error and Exception,Exception into run-time exceptions (RuntimeException and its subclasses) and non-run-time exceptions (compiled exceptions must be manually thrown to catch exceptions, otherwise they may report errors, such as IOException and ClassNotFoundException), so lambda expressions are needed to throw exceptions. I define a functional interface through @ FunctionalInterface, in which the defined method throws IOException and try/catch are used to display the defined exception.

7. Type checking / type inference and restrictions (Callable c = ()-> 42; PrivilegeAction p = ()-> 42; (in Java's security model, used for certain priority judgments). As long as the type conversion required by a functional interface is met, the same Lambda expression can act on different functional interfaces. The diamond operator introduced by Java7 uses the idea of generic inference to infer types from context. (note: special void compatibility: void can be compatible with statement expressions such as: Predicate packs-> list.add (s) and Consume types-> s.add (s)); type inference is the basis on which Lambda can omit specific types

8.Lambda expressions can capture instance and static variables without restrictions, but local variables are limited (must be displayed or implicitly final), that is, local variables assigned to them can be captured only once. Because instance variables are stored in the heap, they can be shared in the heap, but local variables are stored in the stack, implicitly indicating that they are limited to the current thread.

9. Methods reference Comparator::comparing ArrayList::new to the constructor, one variable, supplier=Constructor::new get, two variables, using function=ConStruction::new apply three variables, you can construct the Function function that can accept three variables.

The composition of 10.Lambda expressions (multiple Lambda expressions are combined into complex expressions); 1. Comparator compound Comparing.thenComparing 2. Predicate compound and or (priority from left to right) 3. Function compound f.andThen (g)-g (f (x)) f.compose (g)-f (g (x))

II. Functional data processing

This part is the core of the whole book, and each chapter in it should be read carefully (filter, skip, limit, reduce, map, findAny, forEach,collect).

Third, efficient Java8 programming

1. Default method: this is one of the highlights of Java8.

two。 Introduction of Optional instead of null: introduced from Guava but only a semi-finished product, it is useful to say that it is useful, it is OK to say that it is useless, and it should be an idea to use Optional.

3. New date and time API: introduced from joda but still a semi-finished product, and does not feel any advantage over the current Date&Calendar&Joda, a supplement to the chicken ribs

What is the basic knowledge of java8? the answer to the question is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report