码迷,mamicode.com
首页 > 其他好文 > 详细

JDK8新特性

时间:2018-09-26 00:10:52      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:方法   支持   信息   false   函数式   3.2   stl   语言   需要   

1 Java 8 新特性的简介

  • 速度更快。
  • 代码更少(增加了新语法Lambda表达式)。
  • 强大的Stream API。
  • 便于并行。
  • 最大化的减少空指针异常OPtional。

 

  • 其中最为核心的是Labmda表示式和Stream API。

 

2 为什么使用Lambda表示式

2.1 简介

  • Lambda是一个匿名函数,我们可以把Lambda表示式理解为是一段可以传递的代码。
  • Lambda表示式可以写出更简洁、更灵活的代码。
  • 作为一种更紧凑的代码风格,是java语言的表达能力得到了提升。

2.2 使用java8的演变

  • 使用匿名内部类的方式
package com.xuweiwei;

import org.junit.Test;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class TestLambda {
    @Test
    public void test1(){
        Set<Integer> set = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,02);
            }
        });

        set.add(500);
        set.add(100);

        System.out.println(set);

    }
}
  • 使用Lambda表示式改写上面的匿名内部类的方式
package com.xuweiwei;

import org.junit.Test;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class TestLambda {
   
    @Test
    public void test2(){
        Comparator<Integer> comparator = (o1,o2) ->Integer.compare(o1,o2);

        Set<Integer> set = new TreeSet<>(comparator);

        set.add(-1);
        set.add(500);
        set.add(50);

        System.out.println(set);

    }
}
  • 获取公司员工年龄大于35岁的员工的信息
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class Employee {
    private String name;
    private Integer age;
    private Double salary;

    public Employee() {
    }

    public Employee(String name, Integer age, Double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }
}
package com.xuweiwei;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三",18,1111.11),
            new Employee("李四",28,2222.22),
            new Employee("王五",38,4444.44),
            new Employee("赵六",55,9999.99)
            );

    //需求:获取当前公司中员工年龄大于30的员工信息
    public List<Employee> filterEmployess(List<Employee> employees){
        List<Employee> employeeList = new ArrayList<>();

        for (Employee employee : employees) {
            Integer age = employee.getAge();
            if(null != age && age >= 35){
                employeeList.add(employee);
            }
        }
        return employeeList;
    }

    @Test
    public void test3(){
        List<Employee> employeeList = filterEmployess(employees);
        System.out.println(employeeList);
    }

}
  • 获取公司中员工公司大于5000的员工信息
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class Employee {
    private String name;
    private Integer age;
    private Double salary;

    public Employee() {
    }

    public Employee(String name, Integer age, Double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }
}
package com.xuweiwei;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三",18,1111.11),
            new Employee("李四",28,2222.22),
            new Employee("王五",38,4444.44),
            new Employee("赵六",55,9999.99)
            );

    //需求:获取当前公司中员工年龄大于30的员工信息
    public List<Employee> filterEmployess(List<Employee> employees){
        List<Employee> employeeList = new ArrayList<>();

        for (Employee employee : employees) {
            Integer age = employee.getAge();
            if(null != age && age >= 35){
                employeeList.add(employee);
            }
        }
        return employeeList;
    }

    //获取公司员工工资大于5000的员工信息
    public List<Employee> filterEmployees2(List<Employee> employees){
        List<Employee> employeeList = new ArrayList<>();

        for (Employee employee : employees) {
            Double salary = employee.getSalary();
            if(null != salary && salary >= 5000){
                employeeList.add(employee);
            }
        }
        return employeeList;
    }


    @Test
    public void test3(){
        List<Employee> employeeList = filterEmployees2(employees);
        System.out.println(employeeList);
    }

}
  • 使用设计模式来优化上面的代码
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class Employee {
    private String name;
    private Integer age;
    private Double salary;

    public Employee() {
    }

    public Employee(String name, Integer age, Double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }
}
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public interface MyPredicate<T> {

    boolean test(T t);

}
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class FilterEmployeeByAge implements MyPredicate<Employee> {
    @Override
    public boolean test(Employee employee) {
        if(null != employee){
            Integer age = employee.getAge();
            if(null != age && age >= 35){
                return true;
            }
        }
        return false;
    }
}
package com.xuweiwei;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三",18,1111.11),
            new Employee("李四",28,2222.22),
            new Employee("王五",38,4444.44),
            new Employee("赵六",55,9999.99)
            );

    public List<Employee> filterEmployess(List<Employee> employees,MyPredicate<Employee> mp){
        List<Employee> employeeList = new ArrayList<>();

        for (Employee employee : employees) {
           if(mp.test(employee)){
               employeeList.add(employee);
           }
        }
        return employeeList;
    }


    @Test
    public void test3(){
        List<Employee> employeeList = filterEmployess(employees,new FilterEmployeeByAge());
        System.out.println(employeeList);
    }








}
  • 使用匿名内部类去优化上面的代码
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class Employee {
    private String name;
    private Integer age;
    private Double salary;

    public Employee() {
    }

    public Employee(String name, Integer age, Double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }
}
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public interface MyPredicate<T> {

    boolean test(T t);

}
package com.xuweiwei;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三",18,1111.11),
            new Employee("李四",28,2222.22),
            new Employee("王五",38,4444.44),
            new Employee("赵六",55,9999.99)
            );

    public List<Employee> filterEmployess(List<Employee> employees,MyPredicate<Employee> mp){
        List<Employee> employeeList = new ArrayList<>();

        for (Employee employee : employees) {
           if(mp.test(employee)){
               employeeList.add(employee);
           }
        }
        return employeeList;
    }


    @Test
    public void test3(){
        List<Employee> employeeList = filterEmployess(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee o) {
                if(o.getAge() >35){
                    return true;
                }
                return false;
            }
        });
        System.out.println(employeeList);
    }

}
  • 使用Lambda表达式优化上面的代码
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class Employee {
    private String name;
    private Integer age;
    private Double salary;

    public Employee() {
    }

    public Employee(String name, Integer age, Double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }
}
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public interface MyPredicate<T> {

    boolean test(T t);

}
package com.xuweiwei;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三",18,1111.11),
            new Employee("李四",28,2222.22),
            new Employee("王五",38,4444.44),
            new Employee("赵六",55,9999.99)
            );

    public List<Employee> filterEmployess(List<Employee> employees,MyPredicate<Employee> mp){
        List<Employee> employeeList = new ArrayList<>();

        for (Employee employee : employees) {
           if(mp.test(employee)){
               employeeList.add(employee);
           }
        }
        return employeeList;
    }


    @Test
    public void test3(){
        List<Employee> employeeList = filterEmployess(employees, (e) -> e.getAge() >= 35 );
        employeeList.forEach(System.out::println);
    }


}
  • 使用Stream API优化上面的代码
package com.xuweiwei;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class Employee {
    private String name;
    private Integer age;
    private Double salary;

    public Employee() {
    }

    public Employee(String name, Integer age, Double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getSalary() {
        return salary;
    }

    public void setSalary(Double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }
}
package com.xuweiwei;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生最爱笑
 */
public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 1111.11),
            new Employee("李四", 28, 2222.22),
            new Employee("王五", 38, 4444.44),
            new Employee("赵六", 55, 9999.99)
    );

    @Test
    public void test3() {
        employees.stream().filter((e) -> e.getSalary() >= 2000).limit(2).forEach(System.out::println);
    }


}

 

3 Lambda表达式

3.1 Lambda表达式的基础语法

  • Java8引入了一个新的操作符"->",该操作符称为箭头操作符或Lambda操作符。
  • Lambda表达式分为左右两侧:
  • 左侧:Lambda表达式的参数列表。
  • 右侧:Lambda表达式所需要执行的功能,即Lambda体。

3.1.1 语法格式一

  • 无参数无返回值
() -> System.out.println("hello");

 

  • 示例:
package com.xuweiwei;

import org.junit.Test;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生只爱笑
 */
public class TestLambda2 {
    @Test
    public void test1(){
        //匿名内部类的形式
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("hello world!!");
            }
        };
        //Lambda表达式的方式
        runnable = () -> System.out.println("hello lambda");
        
    }
    
}

3.1.2 语法格式二

  • 有一个参数并且无返回值

 

  • 示例:
package com.xuweiwei;

import org.junit.Test;

import java.util.function.Consumer;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生只爱笑
 */
public class TestLambda2 {
    @Test
    public void test1(){
        Consumer<String> consumer = (e) -> System.out.println(e);
        consumer.accept("你好啊");
    }

}

3.1.3 语法格式三

  • 如果只有一个参数,小括号可以不写

 

  • 示例:
package com.xuweiwei;

import org.junit.Test;

import java.util.function.Consumer;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生只爱笑
 */
public class TestLambda2 {
    @Test
    public void test1(){
        Consumer<String> consumer = e -> System.out.println(e);
        consumer.accept("你好啊");
    }

}

3.1.4 语法格式四

  • 有2个或多个参数,有返回值,并且Lambda体中有多条语句

 

  • 示例:
package com.xuweiwei;

import org.junit.Test;

import java.util.Comparator;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生只爱笑
 */
public class TestLambda2 {
    @Test
    public void test1(){
        Comparator<Integer> comparator = (o1,o2) -> {
            System.out.println("函数式接口");
            
            return Integer.compare(o1,o2);
        };
        
    }

}

3.1.5 语法格式五

  • 有2个或多个参数,有返回值,但是Lambda体中只有一条语句,那么{}和return都可以省略。

 

  • 示例:
package com.xuweiwei;

import org.junit.Test;

import java.util.Comparator;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生只爱笑
 */
public class TestLambda2 {
    @Test
    public void test1(){
        Comparator<Integer> comparator = (x,y) -> Integer.compare(x,y);
        
    }

}

3.1.6 语法格式六

  • Lambda表达式的参数列表的数据类型可以省略不写,因为JVM编译器可以通过上下文推断出类型,即“类型推断”。

 

  • 示例:
package com.xuweiwei;

import org.junit.Test;

import java.util.Comparator;

/**
 * @description:
 * @verion:
 * @since:
 * @motto: 不为往事扰,余生只爱笑
 */
public class TestLambda2 {
    @Test
    public void test1(){
        Comparator<Integer> comparator = (Integer x,Integer y) -> Integer.compare(x,y);

        comparator = (x,y) -> Integer.compare(x,y);
    }

}

3.2 Lambda表示式需要函数式接口的支持

  • 如果一个接口中只有一个抽象方法的接口,就称为函数式接口,可以使用@FunctionalInterface修饰,可以用来检查是否是函数式接口

 

 

 

 

2 函数式接口

 

3 方法引用和构造器引用

 

4 Stream API

 

5 接口中的默认方法和静态方法

 

6 新时间日期API

 

7 其它的新特性

 

JDK8新特性

标签:方法   支持   信息   false   函数式   3.2   stl   语言   需要   

原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9704185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!