In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what are the new features of JDK1.8 in Java language". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the new features of the Java language JDK1.8.
(1) Lambda expression
Pass a function as an argument to a method, or treat the code itself as data.
List list = Arrays.asList ("c", "a", "b")
List.sort ((E1, E2)-> e1.compareTo (e2))
(2) default method, static method and functional interface of interface
A function interface refers to an interface with only one function, which can be implicitly converted to Lambda expressions.
Default and static methods do not break the definition of functional interfaces.
@ FunctionalInterface
Public interface IMyFunTest {
Void testFun ()
Default String getZxwStr () {
Return "zxw"
}
Static void printZxwStr () {
System.out.println ("zxw")
}
}
(3) method reference
Method references are used in conjunction with Lambda expressions to make the constructor of the java class look compact and concise.
Public class Test {
Public static void main (String [] args) throws Exception {
Test test = Test.create (Test::new)
List list = Arrays.asList (test)
List.forEach (Test::staticMethod)
List.forEach (Test::noParamMethod)
List.forEach (new Test ():: instanceMethod)
}
/ / Constructor reference, syntax is Class::new
Public static Test create (Supplier supplier) {
Return supplier.get ()
}
/ / static method reference. Syntax is Class::static_method.
Public static void staticMethod (Test test) {
System.out.println (test.toString ())
}
/ / A reference to the member method of the class. The syntax is Class::method. Note that this method has no input parameters defined.
Public void noParamMethod () {
System.out.println (this.toString ())
}
/ / reference to the member method of the instance object. The syntax is instance::method.
Public void instanceMethod (Test test) {
System.out.println (test.toString ())
System.out.println (this.toString ())
}
}
/ / General application scenarios
List list = Arrays.asList ("a", "c", "b")
List.sort (String::compareTo)
List.forEach (System.out::println); / / a b c
(4) repeated comments
It's not a language improvement, it's a trick made by the compiler, and the underlying technology is still the same.
@ Repeatable (Persons.class)
Public @ interface Person {
String role () default ""
}
@ Target (ElementType.TYPE)
@ Retention (RetentionPolicy.RUNTIME)
Public @ interface Persons {
Person [] value ()
}
@ Person (role= "father")
@ Person (role= "son")
Public class Man {
String name= ""
}
Public static void main (String [] args) throws Exception {
For (Person t: Man.class.getAnnotationsByType (Person.class)) {
System.out.println (t.role ())
}
}
(5) broaden the application scene of annotations.
Annotations can be used on almost any element: local variables, interface types, superclasses, and interface implementation classes, and even on the exception definition of a function.
(6) better type inference
List list = new ArrayList ()
List.add ("a")
List.addAll (new ArrayList ()); / / JDK1.81 was compiled before, but
(7) Optional
Of: encapsulates the specified value as an Optional object, and throws a NullPointerException if the specified value is null
Empty: create an empty Optional object
OfNullable: encapsulates the specified value as an Optional object. If the specified value is null, an empty Optional object is created.
Get: if there is a value in the created Optional, this value is returned, otherwise NoSuchElementException is thrown
OrElse: if a value exists in the created Optional, this value is returned, otherwise a default value is returned
OrElseGet: returns this value if there is a value in the created Optional, otherwise it returns a value generated by the Supplier API
OrElseThrow: if a value exists in the created Optional, this value is returned, otherwise an exception generated by the specified Supplier interface is thrown
Filter: if the value in the created Optional satisfies the condition in filter, the Optional object containing the value is returned, otherwise an empty Optional object is returned
Map: if the value in the created Optional exists, execute the provided Function function call on that value
FlagMap: if a value in the created Optional exists, the provided Function function call is executed on that value and a value of type Optional is returned
Otherwise, an empty Optional object is returned
IsPresent: returns true if the value in the created Optional exists, otherwise returns false
IfPresent: if the value in the created Optional exists, the call to this method is executed, otherwise nothing is done
(8) Streams
Functional programming of the build environment is introduced into the Java library. [relatively complex]
List list = new ArrayList ()
List.addAll (Arrays.asList ("a", "b", "c"))
List.stream () .filter (e->! e.equals ("b")) .forEach (System.out::println)
List strList = Arrays.asList ("a _" .split (",")
Map sortMap = strList.stream () .collect (Collectors.groupingBy (t-> t, Collectors.counting ()
SortMap.forEach ((key, value)-> System.out.println (key + "- >" + value))
(9) Date/Time API
The new time and date management API is deeply influenced by Joda-Time and absorbs a lot of the essence of Joda-Time.
Java.util.time.*
(10) parallel array
Array sorting on multicore machines can be significantly accelerated.
Long [] arrayOfLong = new long [20000]
Arrays.parallelSetAll (arrayOfLong, index-> ThreadLocalRandom.current (). NextInt (1000000)
Arrays.stream (arrayOfLong). Limit (10). ForEach (I-> System.out.print (I + ""))
System.out.println ()
Arrays.parallelSort (arrayOfLong)
Arrays.stream (arrayOfLong). Limit (10). ForEach (I-> System.out.print (I + ""))
(11) Base64 coding support
Java.util.Base64
(12) concurrency
Many new classes and methods have been added to the java.util.concurrent package.
(13) Nashorn JavaScript engine
ScriptEngineManager manager = new ScriptEngineManager ()
ScriptEngine engine = manager.getEngineByName ("JavaScript")
System.out.println (engine.eval ("function f () {return 1;}; f () + 1;")); / / 2.0
(14) Parameter name
Support is provided at the language level (using reflection API and Parameter.getName ()) and at the bytecode level (using the new-parameters parameter).
Maven project configuration:
-parameters
At this point, I believe you have a deeper understanding of "what are the new features of the Java language JDK1.8?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.