In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use the new features of java8". In daily operation, I believe many people have doubts about how to use the new features of java8. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use the new features of java8". Next, please follow the editor to study!
1) Lambda expression concept: a new syntax-Lambda expression has been added to java8, and a new operator (Lambda operator) has been introduced:-> Lambda operator->:-> divides the Lambda expression into two parts: left: specify the parameters required by the Lambda expression The data type of the parameter can be inferred from the compiler's type inference mechanism, so it can be omitted. Right: the method body of the Lambda expression is specified, that is, the function to be performed by the Lambda expression. Syntax format: format 1: no parameters, no return value ()-> System.out.println ("Hello Lambda!") Format 2: has one parameter and no return value (x)-> System.out.println (x) format 3: if there is only one parameter, parentheses can be omitted without writing x-> System.out.println (x) format 4: there are more than two parameters There is a return value, and there are multiple statements (x, y)-> {System.out.println ("functional interface") in the body of Lambda Return Integer.compare (x, y);}; format 5: if there is only one statement in the body of Lambda, return and curly braces can be omitted from (x, y)-> Integer.compare (x, y) Format 6: the data type of the parameter list of the Lambda expression can be omitted because the JVM compiler infers the data type from the context, that is, "type inference" (Integer x, Integer y)-> Integer.compare (x, y) 2) functional interface concept: there is only one interface for abstract methods (except default methods and static methods), which is called functional interface! Add: in java8, there can be default method (default method) and static method (static method) in the interface, and the implementation class inherits defaul method and static method by default. Declare a functional interface: @ FunctionalInterface public interface FirstInterface {void run ();} description: 1) objects with functional interfaces can be created through Lambda expressions. 2) you can use the @ FunctionalInterface annotation on a functional interface so that we can determine whether the interface is a functional interface based on that annotation. 3) four core functional interfaces built into Java8: functional interface parameter type return type abstract method usage consumer interface: Consumer T void void Accept (T t) performs an operation on an object of type T. Supply interface: Supplier has no T T get () returns an object of type T. Functional interface: Function T R R apply (T t) performs an operation on an object of type T and returns the result of type R. Assertive interface: Predicate T boolean boolean test (T t) determines whether an object of type T satisfies a constraint and returns a Boolean value. Eg: / / A body of methods that execute Lambda expressions (x-> System.out.println (x)) on objects of type String. Consumer strConsumer = x-> System.out.println (x); / / create a Consumer (consumer interface) object strConsumer.accept ("accept method of Consumer!") ; / / execute FirstInterface firstObj = ()-> System.out.println ("Hello Lambda!"); firstObj.run () 3) method reference method reference: description: 1) if the function in the body of the Lambda method, there is a method to provide the implementation, you can use method reference. 2) use the operator:: to separate the method name from the name of the object or class. Format: object name:: instance method name class name:: static method name class name:: instance method name eg: Comparator com1 = (x, y)-> Integer.compare (x, y); Comparator com2 = Integer::compare Constructor reference format: class name:: new eg: Supplier sup1 = ()-> new Student (); Supplier sup2 = Student::new Array reference format: type []:: new eg: Function fun1 = (args)-> new String [args]; Function fun2 = String []:: new; 4) Stream API concept: Stream is used to manipulate sets, arrays, and other element sequences. Step 1 of Stream API) create Stream how to create Stream: 1 > create a Stream eg through stream () or parallelStream () in Collection: List list = new ArrayList () Stream stream = list.stream (); / / get a sequential stream Stream parallelStream = list.parallelStream () / / get a parallel stream 2 > create a Stream 3 through stream (T [] array) in Arrays > through of (T) in Stream. Values) create a Stream generate (Supplier s) create an infinite Stream iterate (final T seed) Final UnaryOperator f) create an infinite Stream 2) Stream's intermediate operation Stream filter (Predicate)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.