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

JDK8

时间:2018-10-07 16:02:06      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:filter   code   res   bool   div   gets   out   org   EDA   

1 Java8新特性的简介

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

 

  • 其中最为核心的为Lambda表达式和Stream API

 

2 为什么要使用Lambda表达式

2.1 Lambda表达式的简介

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

2.2 Lambda表达式的演进

2.2.1 使用匿名内部类来实现自定义比较器

  • 示例:
package com.xuweiwei.java8;

import org.junit.Test;

import java.util.Comparator;

public class TestLambda {
    
    @Test
    public void test1(){
        Comparator<Integer> comparator = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(01,02);
            }
        };
    }
    
}

2.2.2 使用Lambda表达式来实现自定义比较器

  • 示例:
package com.xuweiwei.java8;

import org.junit.Test;

import java.util.Comparator;

public class TestLambda {

    @Test
    public void test2(){
        Comparator<Integer> comparator = (x,y) -> Integer.compare(x,y);
    }

}

 

  • 示例:
package com.xuweiwei.java8;

import org.junit.Test;

import java.util.Comparator;

public class TestLambda {

    @Test
    public void test2(){
        Comparator<Integer> comparator = Integer::compare;
    }

}

2.2.3 获取当前公司中员工年龄>35岁的员工信息

  • 示例:
技术分享图片
package com.xuweiwei.java8;

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 +
                ‘}‘;
    }
}
View Code
package com.xuweiwei.java8;

import org.junit.Test;

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

public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 1111.11),
            new Employee("王五", 90, 5555.55),
            new Employee("赵六", 8, 3333.5),
            new Employee("田七", 16, 111.85),
            new Employee("王八", 45, 999.45)
    );


    @Test
    public void test3() {
        List<Employee> employees = filterEmployees(this.employees);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * 获取当前公司中员工年龄>35岁的员工信息
     *
     * @param employees
     * @return
     */
    public List<Employee> filterEmployees(List<Employee> employees) {
        List<Employee> result = new ArrayList<>();

        for (Employee employee : employees) {
            if (employee.getAge() > 35) {
                result.add(employee);
            }
        }
        return result;
    }


}

2.2.4 获取当前公司中员工薪水>5000的员工信息

  • 示例:
技术分享图片
package com.xuweiwei.java8;

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 +
                ‘}‘;
    }
}
View Code
package com.xuweiwei.java8;

import org.junit.Test;

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

public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 1111.11),
            new Employee("王五", 90, 5555.55),
            new Employee("赵六", 8, 3333.5),
            new Employee("田七", 16, 111.85),
            new Employee("王八", 45, 999.45)
    );


    @Test
    public void test3() {
        List<Employee> employees = filterEmployees(this.employees);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * 获取当前公司中员工薪水>5000的员工信息
     *
     * @param employees
     * @return
     */
    public List<Employee> filterEmployees(List<Employee> employees) {
        List<Employee> result = new ArrayList<>();

        for (Employee employee : employees) {
            if (employee.getSalary() > 5000) {
                result.add(employee);
            }
        }
        return result;
    }


}

2.2.5 使用策略模式来改写2.2.3

  • 示例:
技术分享图片
package com.xuweiwei.java8;

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 +
                ‘}‘;
    }
}
View Code
package com.xuweiwei.java8;

public interface MyPredicate<T> {

    boolean test(T t);
}
package com.xuweiwei.java8;

public class FilterEmployeeByAge implements MyPredicate<Employee> {
    @Override
    public boolean test(Employee employee) {
        return employee.getAge() > 35;
    }
}
package com.xuweiwei.java8;

import org.junit.Test;

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

public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 1111.11),
            new Employee("王五", 90, 5555.55),
            new Employee("赵六", 8, 3333.5),
            new Employee("田七", 16, 111.85),
            new Employee("王八", 45, 999.45)
    );


    @Test
    public void test3() {
        List<Employee> employees = filterEmployees(this.employees,new FilterEmployeeByAge());
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * 获取当前公司中员工年龄>35岁的员工信息
     *
     * @param employees
     * @return
     */
    public List<Employee> filterEmployees(List<Employee> employees,MyPredicate<Employee> myPredicate) {
        List<Employee> result = new ArrayList<>();

        for (Employee employee : employees) {
            if (myPredicate.test(employee)) {
                result.add(employee);
            }
        }
        return result;
    }


}

2.2.6 使用策略模式来改写2.2.4 

  • 示例:
技术分享图片
package com.xuweiwei.java8;

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 +
                ‘}‘;
    }
}
View Code
package com.xuweiwei.java8;

public interface MyPredicate<T> {

    boolean test(T t);
}
package com.xuweiwei.java8;

public class FilterEmployeeBySalary implements MyPredicate<Employee> {
    @Override
    public boolean test(Employee employee) {
        return employee.getSalary() > 5000;
    }
}
package com.xuweiwei.java8;

import org.junit.Test;

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

public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 1111.11),
            new Employee("王五", 90, 5555.55),
            new Employee("赵六", 8, 3333.5),
            new Employee("田七", 16, 111.85),
            new Employee("王八", 45, 999.45)
    );


    @Test
    public void test3() {
        List<Employee> employees = filterEmployees(this.employees,new FilterEmployeeBySalary());
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * 获取当前公司中员工薪水>5000的员工信息
     *
     * @param employees
     * @return
     */
    public List<Employee> filterEmployees(List<Employee> employees,MyPredicate<Employee> myPredicate) {
        List<Employee> result = new ArrayList<>();

        for (Employee employee : employees) {
            if (myPredicate.test(employee)) {
                result.add(employee);
            }
        }
        return result;
    }


}

2.2.7 使用匿名内部类改写2.2.5

  • 示例:
技术分享图片
package com.xuweiwei.java8;

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 +
                ‘}‘;
    }
}
View Code
package com.xuweiwei.java8;

public interface MyPredicate<T> {

    boolean test(T t);
}
package com.xuweiwei.java8;

import org.junit.Test;

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

public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 1111.11),
            new Employee("王五", 90, 5555.55),
            new Employee("赵六", 8, 3333.5),
            new Employee("田七", 16, 111.85),
            new Employee("王八", 45, 999.45)
    );


    @Test
    public void test3() {
        List<Employee> employees = filterEmployees(this.employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee employee) {
                return employee.getAge() > 35;
            }
        });
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * 获取当前公司中员工年龄>35岁的员工信息
     *
     * @param employees
     * @return
     */
    public List<Employee> filterEmployees(List<Employee> employees, MyPredicate<Employee> myPredicate) {
        List<Employee> result = new ArrayList<>();

        for (Employee employee : employees) {
            if (myPredicate.test(employee)) {
                result.add(employee);
            }
        }
        return result;
    }


}

2.2.8 使用Lambda表达式改写2.2.5

  • 示例:
技术分享图片
package com.xuweiwei.java8;

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 +
                ‘}‘;
    }
}
View Code
package com.xuweiwei.java8;

public interface MyPredicate<T> {

    boolean test(T t);
}
package com.xuweiwei.java8;

import org.junit.Test;

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

public class TestLambda {

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99),
            new Employee("李四", 38, 1111.11),
            new Employee("王五", 90, 5555.55),
            new Employee("赵六", 8, 3333.5),
            new Employee("田七", 16, 111.85),
            new Employee("王八", 45, 999.45)
    );


    @Test
    public void test3() {
        List<Employee> employees = filterEmployees(this.employees, (e) -> e.getAge() > 35);
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    /**
     * 获取当前公司中员工年龄>35岁的员工信息
     *
     * @param employees
     * @return
     */
    public List<Employee> filterEmployees(List<Employee> employees, MyPredicate<Employee> myPredicate) {
        List<Employee> result = new ArrayList<>();

        for (Employee employee : employees) {
            if (myPredicate.test(employee)) {
                result.add(employee);
            }
        }
        return result;
    }


}

 

JDK8

标签:filter   code   res   bool   div   gets   out   org   EDA   

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

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