java8新特性的主要表現:php
一、Lambda表達式java
二、函數式接口android
三、方法引用和構造器引用ios
四、Stream API設計模式
五、接口中的默認方法與靜態方法app
六、新時間日期APIdom
Lambda表達式:ide
//原來的匿名內部類 @Test public void test1(){ Comparator<String> com = new Comparator<String>(){ @Override public int compare(String o1, String o2) { return Integer.compare(o1.length(), o2.length()); } }; TreeSet<String> ts = new TreeSet<>(com); TreeSet<String> ts2 = new TreeSet<>(new Comparator<String>(){ @Override public int compare(String o1, String o2) { return Integer.compare(o1.length(), o2.length()); } }); }
//如今的 Lambda 表達式 @Test public void test2(){ Comparator<String> com = (x, y) -> Integer.compare(x.length(), y.length()); TreeSet<String> ts = new TreeSet<>(com); }
List<Employee> emps = Arrays.asList( new Employee(101, "張三", 18, 9999.99), new Employee(102, "李四", 59, 6666.66), new Employee(103, "王五", 28, 3333.33), new Employee(104, "趙六", 8, 7777.77), new Employee(105, "田七", 38, 5555.55) ); //需求:獲取公司中年齡小於 35 的員工信息 public List<Employee> filterEmployeeAge(List<Employee> emps){ List<Employee> list = new ArrayList<>(); for (Employee emp : emps) { if(emp.getAge() <= 35){ list.add(emp); } } return list; } @Test public void test3(){ List<Employee> list = filterEmployeeAge(emps); for (Employee employee : list) { System.out.println(employee); } } //需求:獲取公司中工資大於 5000 的員工信息 public List<Employee> filterEmployeeSalary(List<Employee> emps){ List<Employee> list = new ArrayList<>(); for (Employee emp : emps) { if(emp.getSalary() >= 5000){ list.add(emp); } } return list; }
public interface MyPredicate<T> { public boolean test(T t); } public class FilterEmployeeForAge implements MyPredicate<Employee>{ @Override public boolean test(Employee t) { return t.getAge() <= 35; } } public class FilterEmployeeForSalary implements MyPredicate<Employee> { @Override public boolean test(Employee t) { return t.getSalary() >= 5000; } }
//優化方式一:策略設計模式 public List<Employee> filterEmployee(List<Employee> emps, MyPredicate<Employee> mp){ List<Employee> list = new ArrayList<>(); for (Employee employee : emps) { if(mp.test(employee)){ list.add(employee); } } return list; } @Test public void test4(){ List<Employee> list = filterEmployee(emps, new FilterEmployeeForAge()); for (Employee employee : list) { System.out.println(employee); } System.out.println("------------------------------------------"); List<Employee> list2 = filterEmployee(emps, new FilterEmployeeForSalary()); for (Employee employee : list2) { System.out.println(employee); } }
//優化方式二:匿名內部類 @Test public void test5(){ List<Employee> list = filterEmployee(emps, new MyPredicate<Employee>() { @Override public boolean test(Employee t) { return t.getId() <= 103; } }); for (Employee employee : list) { System.out.println(employee); } }
//優化方式三:Lambda 表達式 @Test public void test6(){ List<Employee> list = filterEmployee(emps, (e) -> e.getAge() <= 35); list.forEach(System.out::println); System.out.println("------------------------------------------"); List<Employee> list2 = filterEmployee(emps, (e) -> e.getSalary() >= 5000); list2.forEach(System.out::println); }
//優化方式四:Stream API @Test public void test7(){ emps.stream() .filter((e) -> e.getAge() <= 35) .forEach(System.out::println); System.out.println("----------------------------------------------"); emps.stream() .map(Employee::getName) .limit(3) .sorted() .forEach(System.out::println); }
Lambda表達式的基礎語法:函數
->:稱爲箭頭操做符,或者lambda操做符,箭頭操做符把表達式拆分紅兩份學習
左側:表達式的參數列表
右側:表達式須要執行的功能, 即Lambda體
語法格式一:無參數,無返回值 () -> System.out.println("Hello Lambda!");
@Test public void test1(){ int num = 0;//jdk 1.7 前,必須是 final Runnable r = new Runnable() { @Override public void run() { System.out.println("Hello World!" + num); } }; r.run(); System.out.println("-------------------------------"); Runnable r1 = () -> System.out.println("Hello Lambda!"); r1.run(); }
語法格式二:有一個參數,而且無返回值 (x) -> System.out.println(x)
@Test public void test2(){ Consumer<String> con = (x) -> System.out.println(x); con.accept("好好學習,每天向上!"); }
語法格式三:若只有一個參數,小括號能夠省略不寫 x -> System.out.println(x)
@Test public void test2(){ Consumer<String> con = x -> System.out.println(x); con.accept("xxx"); }
語法格式四:有兩個以上的參數,有返回值,而且 Lambda 體中有多條語句
@Test public void test3(){ Comparator<Integer> com = (x, y) -> { System.out.println("xxxx"); return Integer.compare(x, y); }; }
語法格式五:若 Lambda 體中只有一條語句, return 和 大括號均可以省略不寫
@Test public void test4(){ Comparator<Integer> com = (x, y) -> Integer.compare(x, y); }
語法格式六:Lambda 表達式的參數列表的數據類型能夠省略不寫,由於JVM編譯器經過上下文推斷出,數據類型,即「類型推斷」 (Integer x, Integer y) -> Integer.compare(x, y);
@Test public void test5(){ String[] strs = {"aaa", "bbb", "ccc"}; List<String> list = new ArrayList<>(); show(new HashMap<>()); } public void show(Map<String, Integer> map){ }
lambda表達式須要函數式接口的支持:
函數式接口:接口中只有一個抽象方法的接口,稱爲函數式接口,可使用註解 @FunctionalInterface 修飾,能夠檢查是不是函數式接口
@FunctionalInterface public interface Fun { public Integer getValue(Integer num); }
//需求:對一個數進行運算 @Test public void test6(){ Integer num = operation(100, (x) -> x * x); System.out.println(num); System.out.println(operation(200, (y) -> y + 200)); } public Integer operation(Integer num, Fun mf){ return mf.getValue(num); }
//比較兩個實體,先按照年齡比較,相同則按照姓名比較 List<Employee> emps = Arrays.asList( new Employee(101, "張三", 18, 9999.99), new Employee(102, "李四", 59, 6666.66), new Employee(103, "王五", 28, 3333.33), new Employee(104, "趙六", 8, 7777.77), new Employee(105, "田七", 38, 5555.55) ); @Test public void test1(){ Collections.sort(emps, (e1, e2) -> { if(e1.getAge() == e2.getAge()){ return e1.getName().compareTo(e2.getName()); }else{ return -Integer.compare(e1.getAge(), e2.getAge()); } }); for (Employee emp : emps) { System.out.println(emp); } }
//字符串操做
@FunctionalInterface public interface MyFunction { public String getValue(String str); } //需求:用於處理字符串 public String strHandler(String str, MyFunction mf){ return mf.getValue(str); } @Test public void test2(){ String trimStr = strHandler("\t\t\tjava天下第一 ", (str) -> str.trim()); System.out.println(trimStr); String upper = strHandler("abcdef", (str) -> str.toUpperCase()); System.out.println(upper); String newStr = strHandler("PHP天下第一", (str) -> str.substring(2, 5)); System.out.println(newStr); }
public interface MyFunction2<T, R> { public R getValue(T t1, T t2); } //需求:對於兩個 Long 型數據進行處理 public void op(Long l1, Long l2, MyFunction2<Long, Long> mf){ System.out.println(mf.getValue(l1, l2)); } @Test public void test3(){ op(100L, 200L, (x, y) -> x + y); op(100L, 200L, (x, y) -> x * y); }
、
java8中內置的四大函數式接口
Customer<T> 消費型接口
void accept( T t);
//Consumer<T> 消費型接口 : @Test public void test1(){ happy(10000, (m) -> System.out.println("這次購物消費" + m + "元")); } public void happy(double money, Consumer<Double> con){ con.accept(money); }
Supplier( T t ) 供給型接口
T get();
//Supplier<T> 供給型接口 : @Test public void test2(){ List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100)); for (Integer num : numList) { System.out.println(num); } } //需求:產生指定個數的整數,並放入集合中 public List<Integer> getNumList(int num, Supplier<Integer> sup){ List<Integer> list = new ArrayList<>(); for (int i = 0; i < num; i++) { Integer n = sup.get(); list.add(n); } return list; }
Function<T, R>:函數型接口
R apply(T t);
//Function<T, R> 函數型接口: @Test public void test3(){ String newStr = strHandler("\t\t\t 我大尚硅谷威武 ", (str) -> str.trim()); System.out.println(newStr); String subStr = strHandler("我大尚硅谷威武", (str) -> str.substring(2, 5)); System.out.println(subStr); } //需求:用於處理字符串 public String strHandler(String str, Function<String, String> fun){ return fun.apply(str); }
Predicate<T>:斷言型接口
boolean test(T t);
//Predicate<T> 斷言型接口: @Test public void test4(){ List<String> list = Arrays.asList("java", "php", "android", "ios", ".net"); List<String> strList = filterStr(list, (s) -> s.length() > 4); for (String str : strList) { System.out.println(str); } } //需求:將知足條件的字符串,放入集合中 public List<String> filterStr(List<String> list, Predicate<String> pre){ List<String> strList = new ArrayList<>(); for (String str : list) { if(pre.test(str)){ strList.add(str); } } return strList; }