Java8新特性
推薦閱讀:
- CSDN-雲深不知處:Java8 Stream:2萬字20個執行個體,玩轉集合的篩選、歸約、分組、聚合
- CSDN-高可用架構:你可能未曾使用的新 Java 特性
- lambda表達式、方法引用、構造器引用、數組引用
- Stream流
- optional
- 時間日期API [線程安全]
- 重複注解與類型注解
- ForkJoin架構
- 接口的預設方法和靜态方法
1. Lambda表達式
Java8中引入了一個新的操作符 “->” 該操作符稱位箭頭操作符
- 定義一個接口(函數式接口)
@FunctionalInterface
public interface MyFunction {
Integer getValue(Integer num);
}
- 使用lambda表達式
public class LambdaTest2 {
public Integer operation(Integer num, MyFunction mf) {
return mf.getValue(num);
}
@Test
public void test1() {
Integer num = operation(100, (x) -> x * x);
System.out.println(num);
Integer num2 = operation(100, (x) -> x + 200);
System.out.println(num2);
}
}
1.1 内置四大核心函數式接口
接口名稱 | 接口類型 | 方法 | 方法參數 | 方法傳回值類型 |
---|---|---|---|---|
Consumer | 消費型 | void accept(T t) | √ | × |
Supplier | 供給型 | T get() | × | √ |
Function<T,R> | 函數型 | R apply (T t) | √ | √ |
Predicate | 斷言型 | boolean test(T t) | √ | √ |
消費型接口
/**
* Consumer<T>: 消費型接口
* 需求:
*/
public void happy(double money, Consumer<Double> con) {
con.accept(money);
}
@Test
public void happyTest() {
happy(10000, m -> System.out.println("今天吃飯花了" + m));
}
供給型接口
/**
* Supplier<T>:供給型接口
* 需求:随機存儲10個數字存入集合
*/
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);
}
System.out.println(list);
return list;
}
@Test
public void supTest() {
getNumList(10, () -> (int) (Math.random() * 100));
}
函數型接口
/**
* Function<T,R>: 函數型接口
*/
public String strHandler(String str, Function<String, String> fun) {
return fun.apply(str);
}
@Test
public void funTest() {
String s = strHandler("\t\t\t 你是誰啊! \t\t\t", (str) -> str.trim());
System.out.println(s);
}
斷言型接口
/**
* Predicate<T>: 斷言型接口
* 需求:将滿足條件的字元串放入集合
*/
public static List<String> filterStr(List<String> strList, Predicate<String> pre) {
List<String> list = new ArrayList<>();
for (String str : strList) {
if (pre.test(str)) {
list.add(str);
}
}
return list;
}
@Test
public void preHandler(){
List<String> stringList = Arrays.asList("Hello", "Friends", "Lambda", "www", "ok");
List<String> strings = filterStr(stringList, (str) -> str.length() > 3);
System.out.println(strings);
}
1.2 方法引用
若Lambda體中的内容有方法已經實作了,我們可以使用“方法引用”(可了解為是lambda表達式的另外一種表現形式)
有三種文法形式
- 對象::執行個體方法名
- 類名::靜态方法名
- 類名::執行個體方法名
注意
- Lambda體中調用方法的參數清單與傳回值類型,要與函數式接口中抽象方法的函數清單和傳回值類型保持一緻!
- 若Lambda參數清單中的第一參數是執行個體方法的調用者,而第二個參數是執行個體方法的參數時,可使用 ClassName::methodName
對象::執行個體方法名
/**
* 對象::執行個體方法名
*/
@Test
public void test1() {
PrintStream ps = System.out;
// 方法引用
Consumer<String> con = ps::println;
con.accept("aqwsdasagas");
}
類名::靜态方法名
/**
* 類::靜态方法名
*/
@Test
public void test2() {
// lambda
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
// 方法引用
Comparator<Integer> com1 = Integer::compare;
}
類::執行個體方法名
/**
* 類::執行個體方法名
*/
@Test
public void test3() {
BiPredicate<String, String> bp = (x, y) -> x.equals(y);
BiPredicate<String, String> bp1 = String::equals;
}
@Test
public void test2() {
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
Comparator<Integer> com1 = Integer::compare;
}
1.3 構造器引用
注意:需要調用的構造函數的參數清單要與函數式接口中抽象方法的參數清單保持一緻!
前提定義Employee和枚舉類
- Employee
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private int id;
private String name;
private int age;
private double salary;
private Status status;
public Employee(int id) {
this.id = id;
}
}
- 構造器引用案例
/**
* 構造器引用: 獲得了一個無參構造
*/
@Test
public void test4() {
// lambda
Supplier<Employee> sup = () -> new Employee();
// 構造器引用
Supplier<Employee> sup2 = Employee::new;
// 應該獲得了一個無參構造,因為get方法就是無參有傳回值
Employee employee = sup2.get();
System.out.println(employee);
}
/**
* 構造器引用:擷取參數為int id的構造方法
*/
@Test
public void test5() {
Function<Integer, Employee> fun = (x) -> new Employee(x);
Employee apply = fun.apply(2);
Function<Integer, Employee> fun2 = Employee::new;
// 根據apply的參數個數去調用對應的構造方法(一個參數,int類型)
Employee apply2 = fun.apply(3);
System.out.println(apply);
System.out.println(apply2);
}
1.4 數組引用
Type::new
/**
* 數組引用
*/
@Test
public void test7(){
Function<Integer,String[]> fun = (x) -> new String[x];
String[] strs = fun.apply(10);
System.out.println(strs.length);
Function<Integer,String[]> fun2 = String[]::new;
System.out.println(fun2.apply(15).length);
}
1.5 情景處理
某公司員工定制排序
package cn.luis.lambda.practise;
import cn.luis.lambda.method.Employee;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Compony {
/**
* 某公司員工基本資訊
*/
List<Employee> emps = Arrays.asList(
new Employee(1,"zhanagsan", 18, 1111.11),
new Employee(2,"lisi", 49, 2222.22),
new Employee(3,"zhaoliu", 49, 3333.33),
new Employee(4,"tianqi", 35, 4444.44)
);
/**
* 定制排序:先按年齡比,年齡相同按姓名比
*/
@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());
}
});
System.out.println("======================");
for (Employee emp : emps) {
System.out.println(emp);
}
}
}
字元串處理
package cn.luis.lambda.practise;
import org.junit.jupiter.api.Test;
public class StringTest {
/**
* 函數式接口
*/
interface StringFun {
String getValue(String str);
}
/**
* 用于處理字元串
*/
public String strHandler(String str, StringFun sf) {
return sf.getValue(str);
}
/**
* 字元串處理
*/
@Test
public void test1() {
String s1 = strHandler("\t\t\t hello , may i have a dog \t\t\t", str -> str.trim());
System.out.println(s1);
String s2 = strHandler("asdfghjkl", str -> str.toUpperCase());
System.out.println(s2);
String s3 = strHandler("asdfghjkl", str -> str.substring(0, 3));
System.out.println(s3);
}
}
long型數字處理
package cn.luis.lambda.practise;
import org.junit.jupiter.api.Test;
public class LongTest {
/**
* 函數式接口
*/
public interface MyFunction<T, R> {
R getValue(T t1, T t2);
}
/**
* 對于兩個long型資料進行處理
* MyFunction<Integer,Integer>: 傳入參數類型、傳回值類型
*/
public void operacte(Integer x, Integer y, MyFunction<Integer, Integer> mf) {
System.out.println(mf.getValue(x, y));
}
@Test
public void test1() {
operacte(100, 100, (x, y) -> x + y);
}
}