23 jdk8 new features

problem

What new features have jdk8

answer

  • Lambda expressions - Lambda allowed to function as an argument of a method.
    Before using the lambda expression is this:
        Integer[] is=new Integer[]{43,65,87,324,76,12,61,13};
        Arrays.sort(is, new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });

After using the lambda expression like this:

        Integer[] is=new Integer[]{43,65,87,324,76,12,61,13};
        Arrays.sort(is, (o1,o2) -> o2-o1);
        System.out.println(Arrays.toString(is));
  • Method References - Referenced Java classes or objects (instances) method or configuration, a configuration such that a more compact and simple language.
        List names = new ArrayList();
        names.add("ali");
        names.add("xiaoli");
        names.add("zhangli");
        names.forEach(System.out::println);
  • The default method - the default method is a method in the interface which has been achieved.
public interface UserService {

    // static修饰符定义静态方法,只能通过接口名称来调用
    static void staticMethod() {
        System.out.println("接口中的静态方法");
    }

    // default修饰符定义默认方法,只能通过接口实现类的对象来调用
    default void defaultMethod() {
        System.out.println("接口中的默认方法");
    }
}
  • Stream API - streaming programming or functional programming interface provides a four base
    (1) Supplier <T>: data provider, it may provide an object of type T; no argument constructor, a get method;
    (2) Function <T, R>: a data converter which receives an object of type T, returns an object of type R; single data sheet return behavior value interface; providing apply, compose, andThen, identity methods;
    (. 3) Consumer < T>: data consumer which receives a object of type T and returns no value, generally sets the value T of the object; no single parameter value returns the interface behavior; providing accept, andThen method;
    (. 4) the Predicate <T> : test conditions which receives an object of type T, returns a Boolean value, conditions usually used for transfer function; conditionally interface unit boolean parameter. Providing test (test conditions), and-or- negate (or with) method. Wherein, compose, andThen, and, or , negate function interface for combining the obtained more powerful function interface.
  • Optional classes - solve null pointer exception.
  • Date Time API - to strengthen the handling of date and time.
  • JavaScript engine Nashorn, class dependent parser jdeps

Guess you like

Origin www.cnblogs.com/alichengxuyuan/p/12620979.html