1 lambda表達式
//原來的匿名內部類
@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);
}
//employee的屬性
//private int id;
//private String name;
//private int age;
//private double salary;
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)
);
lambda表達式的書寫規範
/*
* 一、Lambda 表達式的基礎語法:Java8中引入了一個新的操作符 "->" 該操作符稱爲箭頭操作符或 Lambda 操作符
* 箭頭操作符將 Lambda 表達式拆分成兩部分:
*
* 左側:Lambda 表達式的參數列表
* 右側:Lambda 表達式中所需執行的功能, 即 Lambda 體
*
* 語法格式一:無參數,無返回值
* () -> System.out.println("Hello Lambda!");
*
* 語法格式二:有一個參數,並且無返回值
* (x) -> System.out.println(x)
*
* 語法格式三:若只有一個參數,小括號可以省略不寫
* x -> System.out.println(x)
*
* 語法格式四:有兩個以上的參數,有返回值,並且 Lambda 體中有多條語句
* Comparator<Integer> com = (x, y) -> {
* System.out.println("函數式接口");
* return Integer.compare(x, y);
* };
*
* 語法格式五:若 Lambda 體中只有一條語句, return 和 大括號都可以省略不寫
* Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
*
* 語法格式六:Lambda 表達式的參數列表的數據類型可以省略不寫,因爲JVM編譯器通過上下文推斷出,數據類型,即“類型推斷”
* (Integer x, Integer y) -> Integer.compare(x, y);
*
* 上聯:左右遇一括號省
* 下聯:左側推斷類型省
* 橫批:能省則省
* 右邊只有一行的時候return也可以省略
*
* 二、Lambda 表達式需要“函數式接口”的支持
* 函數式接口:接口中只有一個抽象方法的接口,稱爲函數式接口。 可以使用註解 @FunctionalInterface 修飾
* 可以檢查是否是函數式接口
*/
/*
* Java8 內置的四大核心函數式接口
*
* Consumer<T> : 消費型接口
* void accept(T t);
*
* Supplier<T> : 供給型接口
* T get();
*
* Function<T, R> : 函數型接口
* R apply(T t);
*
* Predicate<T> : 斷言型接口
* boolean test(T t);
*
*/
public class TestLambda3 {
//Predicate<T> 斷言型接口:
@Test
public void test4(){
List<String> list = Arrays.asList("Hello", "atguigu", "Lambda", "www", "ok");
List<String> strList = filterStr(list, (s) -> s.length() > 3);
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;
}
//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);
}
//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;
}
//Consumer<T> 消費型接口 :
@Test
public void test1(){
happy(10000, (m) -> System.out.println("你們剛哥喜歡大寶劍,每次消費:" + m + "元"));
}
public void happy(double money, Consumer<Double> con){
con.accept(money);
}
}