Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java eight new features 8     - Impact test noatime Linux file access time (Linux)

- Linux System Getting Started Learning: the local time in between Perl and UNIX timestamp conversion (Linux)

- JavaScript common array manipulation functions and usage (Programming)

- Prevent security threats caused Rootkit (Linux)

- Virtualization and IT cooperation (Linux)

- Switching Virtual Machine + Ubuntu graphical interface and terminal interface (Linux)

- Oracle () trunc function usage (Database)

- DB2 commonly used scripting sort out (Database)

- Difference between TCP and UDP protocols (Linux)

- Linux operation and maintenance of the automated installation and deployment RHEL7.0 Cobbler (Linux)

- Java logging performance of those things (Programming)

- java.net.NoRouteToHostException of Hadoop problem: no route to host (Server)

- Embedded Linux Optimization (Programming)

- Permissions and attributes of files and directories under Linux (Linux)

- A command to install Sublime Text 3 on Manjaro / Archlinux (Linux)

- MariaDB 10.0.X, the dynamic column support JSON format to obtain data (Database)

- MySQL dual master configuration (Database)

- Linux / Centos anti CC attack script (Linux)

- The most commonly used Linux commands (Linux)

- Protobuf compiled and used on the Ubuntu 14.04 (Programming)

  Java eight new features 8
  Add Date : 2018-11-21      
  1, The default interface methods and static methods
Java 8 in the interface introduces new features.
Before Java version 8, the interface only abstract methods, but in Java 8, the interface adds two types of methods.
The first one is the default method. This method uses the default keyword modified the method name. In fact, the interface does not contain any methods implemented in Java 8, you can add a default method by using the default keyword implementation.
Class that implements the interface can directly use these default methods, but also can override the default method, which is not mandatory rewritten.

package demo.ch;

public interface Java8InterfaceDemo {
    abstract void add ();
    default void display () {
        System.out.println ( "default method of interface");
Java 8 Interface introduced the second method is a static method.
This class is similar to the static method, you can use the static keyword static methods defined in the interface. If we want to call static methods defined by the interface, simply use the interface name to access these static methods. such as:

package demo.ch;

public interface Java8InterfaceDemo {
    abstract void add ();
    default void display () {
        System.out.println ( "default method of interface");
    public static void show () {
        System.out.println ( "static method of interface");
2, Lambda expressions
Java 8 The most exciting feature is the Lambda expressions, it is able to function / action passed as a parameter to the method.

package demo.ch;

import java.util.Arrays;

public class JavalamdaExpression {
    public static void main (String [] args) {
        Arrays.asList ( "j", "a", "v", "a", "8") forEach (e-> System.out.print (e)).;
3, java.util.Optional class
Java 8 Added Optional classes in the java.util package, Optional class is a container that may or may not contain non-null value of the object. Every Java project, the most important statement is the repeated check for null pointer exception NullPointerException. We use any object, you need to check whether this object is empty, if the object is not empty before we execute processing statement.
Optional class like a container that holds a value of type or null value. By using the Optional class isPresent () method, we can check whether the specified object is empty.

package demo.ch;

import java.util.Optional;

public class Java8OptionalDemo {
    public static void main (String [] args) {
        Optional str = Optional.ofNullable (null);
        System.out.println ( "str having value?" + Str.isPresent ());
        // Output: str having value false?
4, Streams
Streaming API is Java 8 introduces a proven functional programming, Stream API provides functions operation flow elements, including list, set, map, etc., but also supports filtering filtering, mapping mapping, remove the collection of duplicate elements and the like.
Stream flow can be obtained from the collection, arrays, the read buffer and the like.

package demo.ch;

import java.util.Arrays;

public class Java8StreamsDemo {
    public static void main (String [] args) {
        Arrays.stream (new int [] {1, 2, 3, 4, 5})
            .map (n-> 2 * n + 1)
            .average ()
            .ifPresent (System.out :: println);
        // Output: 7.0
5, a reference method
We can use Lambda expressions to create an anonymous method. However, Lambda expressions do not just call the existing methods. In some cases, it can also search for specific use by specifying the name of an existing method. By name to use reference code is more compact and easier to read.

package demo.ch;

import java.util.Arrays;

public class Java8MethodRef {
    public static void main (String [] args) {
        Arrays.asList ( "a", "b", "c") forEach (new Java8MethodRef () :: show).;
    public void show (String str) {
        System.out.print (str + "");
    // Output: a b c
6, the date and time API
Java 8 uses the JSR 310 specification, added java.time package.
Before Java version 8, if we want to format the date, you must use the SimpleDateFormat class, use it to enter the date format class. The Java 8 introduces the following new class date and time:


package demo.ch;

import java.time.LocalDate;
import java.time.Month;

public class Java8DateTimeAPI {
    public static void main (String [] args) {
        LocalDate currentDate = LocalDate.now ();
        System.out.println (currentDate);
        // Output: 2015-11-24
        LocalDate twentyDecember2015 = LocalDate.of (2015, Month.DECEMBER, 20);
        System.out.println (twentyDecember2015);
        // Output: 2015-12-20
        LocalDate firstDec2015 = LocalDate.of (2015, 12, 1);
        System.out.println (firstDec2015);
        // Output: 2015-12-01
7, Nashorn Javascript engine
Java 8 introduces a new Nashorn Javascript engine, with which you can develop and run JavaScript applications.

package demo.ch;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Java8JavaScript {
    public static void main (String [] args) {
        ScriptEngineManager manager = new ScriptEngineManager ();
        ScriptEngine engine = manager.getEngineByName ( "JavaScript");
        System.out.println (engine.getClass () getName ().);
        try {
            System.out.println ( "output:" + engine.eval ( "function show () {return 10;}; show ();"));
        } Catch (ScriptException e) {
            e.printStackTrace ();
        // Jdk.nashorn.api.scripting.NashornScriptEngine
        // Output: 10
8, an array of parallel sorting
In Java 7 has been Arrays.sort method can sort objects (), whereas in Java 8, the introduction of a new parallel sort, sort it faster than the former, and followed the introduction of the Java 7 Fork / Join framework, sorting task can be assigned to multiple threads in the pool is available.
Java 8 new parallel sorting function java.util.Arrays class, can make better use of multi-threading mechanism.

package demo.ch;

import java.util.Arrays;

public class ParallelSort {
    public static void main (String [] args) {
        int arr [] = {1, 4, 2, 8, 5};
        Arrays.parallelSort (arr);
        for (int i: arr) {
            System.out.print (+ "");
    // Output: 1 2 4 5 8
- Nginx logging client ip (Server)
- Git large file storage will help handle large binary files (Linux)
- Linux System Administrator common interview questions and answers 30 (Linux)
- Oracle 10g New Features - Archive Compression (Database)
- The difference between statement and preparedStatement of the jdbc (Database)
- Ubuntu 14.04 solved using MyEclipse 10.7 flash back (Linux)
- grep command Series: How to Use the grep command in Linux / UNIX (Linux)
- Vim Getting Started Tutorial (Linux)
- Install NetBeans IDE 8.0 on Ubuntu, Linux Mint, Elementary OS, and Debian (Linux)
- How to create a new file system / partitions under Linux terminal (Linux)
- To_explore Linux system boot process (Linux)
- Let the terminal under Mac OS X as like Linux has displayed a variety of colors (Linux)
- Install Rubinius testing Ubuntu 14.04 native threads (Linux)
- CentOS6.7 text installation system (Linux)
- Android development, may cause a memory leak problem (Programming)
- Java deserialization test (Programming)
- Oracle RMAN backups of the control file backup (Database)
- Linux ldconfig command (Linux)
- Simple to use Linux GPG (Linux)
- Oracle 11g statistics collection - collection of multi-column statistics (Database)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.