日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java compare 返回值_关于Java你不知道的那些事之Java8新特性[Lambda表达式和函数式接口]...

發(fā)布時間:2025/3/21 java 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java compare 返回值_关于Java你不知道的那些事之Java8新特性[Lambda表达式和函数式接口]... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

前言

為什么要用Lambda表達式?
Lambda是一個匿名函數(shù),我們可以把Lambda表達式理解為是一段可以傳遞的代碼,將代碼像數(shù)據(jù)一樣傳遞,這樣可以寫出更簡潔、更靈活的代碼,作為一個更緊湊的代碼風(fēng)格,使Java語言表達能力得到了提升

實例代碼

Lambda表達式最先替代的就是匿名內(nèi)部類,假設(shè)原來我們寫一個Comparator比較函數(shù),采用匿名內(nèi)部類的方式

/*** 原來使用匿名內(nèi)部類*/public static void test() {// 使用匿名內(nèi)部類,重寫Intger的 compare方法Comparator<Integer> comparator = new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return Integer.compare(o1, o2);}};// 傳入比較的方法TreeSet<Integer> ts = new TreeSet<>(comparator);}

然后在采用Lambda表達式后

/*** 使用Lambda表達式解決匿名內(nèi)部類需要編寫大量模板語言的問題*/public static void test2() {Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);// 傳入比較的方法TreeSet<Integer> ts = new TreeSet<>(comparator);}

策略設(shè)計模式

假設(shè)我們現(xiàn)在有一個需求,就是查找出員工里面年齡超過35的,我們使用策略設(shè)計模式

/*** 員工類** @author: 陌溪* @create: 2020-04-05-12:13*/ public class Employee {private String name;private int age;private double salary;// set get }

然后創(chuàng)建一個接口,這里就是判定條件

/*** 接口* @param <T>*/ public interface MyPredicte<T> {public boolean test(T t); }

我們實現(xiàn)這個接口

/*** 按年齡過濾** @author: 陌溪* @create: 2020-04-05-12:23*/ public class FilterEmployeeByAge implements MyPredicte<Employee> {@Overridepublic boolean test(Employee employee) {return employee.getAge() > 35;} }

然后在具體的例子中使用

/*** 獲取當(dāng)前公司員工年齡大于35*/public static void test3() {List<Employee> employees = Arrays.asList(new Employee("張三", 18, 3333),new Employee("李四", 38, 55555),new Employee("王五", 50, 6666.66),new Employee("趙六", 16, 77777.77),new Employee("田七", 8, 8888.88));MyPredicte<Employee> mp = new FilterEmployeeByAge();List<Employee> emps = new ArrayList<>();for (Employee emp : emps) {if(mp.test(emp)) {emps.add(emp);}}}

當(dāng)某一天需求變更了,變成需要查找金額大于60000的,那么只需要在編寫一個實現(xiàn)類即可

/*** 按薪資過濾** @author: 輕狂書生FS* @create: 2020-10-05-12:23*/ public class FilterEmployeeBySalary implements MyPredicte<Employee> {@Overridepublic boolean test(Employee employee) {return employee.getSalary() > 60000;} }

那么具體使用只需要更改為

/*** 獲取當(dāng)前公司薪資大于60000*/public static void test3() {List<Employee> employees = Arrays.asList(new Employee("張三", 18, 3333),new Employee("李四", 38, 55555),new Employee("王五", 50, 6666.66),new Employee("趙六", 16, 77777.77),new Employee("田七", 8, 8888.88));MyPredicte<Employee> mp = new FilterEmployeeBySalary();List<Employee> emps = new ArrayList<>();for (Employee emp : emps) {if(mp.test(emp)) {emps.add(emp);}}}

這樣一個方法,被稱為策略設(shè)計模式

匿名內(nèi)部類

使用上面的策略設(shè)計模式,我們會發(fā)現(xiàn)一個問題,就是每當(dāng)我需要增加一個條件的時候,就需要增加一個實現(xiàn)類,如果條件多了的話,那么就會有很多實現(xiàn)類,那么為了優(yōu)化,我們可以采取匿名內(nèi)部類的方式

/*** 優(yōu)化方式,采用匿名內(nèi)部類的方式*/public static void test5() {List<Employee> employees = Arrays.asList(new Employee("張三", 18, 3333),new Employee("李四", 38, 55555),new Employee("王五", 50, 6666.66),new Employee("趙六", 16, 77777.77),new Employee("田七", 8, 8888.88));// 匿名內(nèi)部類filterEmployee(employees, new MyPredicte<Employee>() {@Overridepublic boolean test(Employee employee) {return employee.getSalary() <= 5000;}});}

直接在內(nèi)部類中,使用我們的過濾條件

Lambda表達式

/*** 使用Lambda表達式優(yōu)化*/public static void test6() {List<Employee> employees = Arrays.asList(new Employee("張三", 18, 3333),new Employee("李四", 38, 55555),new Employee("王五", 50, 6666.66),new Employee("趙六", 16, 77777.77),new Employee("田七", 8, 8888.88));List<Employee> list = filterEmployee(employees, (e) -> e.getSalary() <= 5000);list.forEach(System.out::println);}

或者

/*** 不使用策略模式*/public static void test7() {List<Employee> employees = Arrays.asList(new Employee("張三", 18, 3333),new Employee("李四", 38, 55555),new Employee("王五", 50, 6666.66),new Employee("趙六", 16, 77777.77),new Employee("田七", 8, 8888.88));employees.stream().filter(e-> e.getSalary() >= 5000).limit(2).forEach(System.out::println);System.out.println("=========");employees.stream().map(Employee::getName).forEach(System.out::println);}

學(xué)習(xí)Lambda

Lambda表達式基礎(chǔ)語法:Java8中引入了一個新的操作符 “->” 該操作符稱為箭頭操作符 或 Lambda操作符

箭頭操作符將Lambda表達式拆分為兩部分:

  • 左側(cè):Lambda表達式的參數(shù)列表(可以想象成,是上面定義的接口中抽象方法參數(shù)的列表)
  • 右側(cè):Lambda表達式中,所需要執(zhí)行的功能,即Lambda體(需要對抽象方法實現(xiàn)的功能)

語法格式

1、無參,無返回值

格式:

() -> System.out.println(“hello”);

舉例:

public static void test() {Runnable r = new Runnable() {@Overridepublic void run() {System.out.println("hello");}};System.out.println("=========");Runnable runnable = () -> {System.out.println("hello lambda");};}

JDK1.8以后,調(diào)用Lambda外的值,不需要增加final字段,它默認已經(jīng)添加了final

int n = 10; Runnable runnable = () -> {System.out.println("hello lambda" + n); };

2、有一個參數(shù),有返回值

格式:

(x) -> System.out.println(x); 或 (一個參數(shù)時,小括號可以省略不寫) x -> System.out.println(x);

實例:

public static void test2() {Consumer<String> consumer = (x) -> System.out.println(x);consumer.accept("我在bilibili"); }

3、有多個參數(shù),一個返回值

/*** 多個參數(shù),有返回值*/public static void test3() {Comparator<Integer> comparator = (x, y) -> {System.out.println("函數(shù)式接口");return Integer.compare(x, y);};}

4、有多個參數(shù),只有一條語句

這個時候,可以省略大括號 和 return

/*** 多個參數(shù),函數(shù)體只有一條,并且有返回值時*/public static void test4() {Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);}

類型推斷

Lambda中,表達式的參數(shù)列表的數(shù)據(jù)類型可以省略不寫,因為JVM編譯器通過上下文推斷出,數(shù)據(jù)類型,即“類型推斷”。

(Integer x, Integer y) -> Integer.compare(x, y);

但是底層的類型檢查還是有的,只是JDK底層幫我們做了類型檢查這件事

函數(shù)式接口

Lambda表達式需要“函數(shù)式接口”的支持

函數(shù)式接口:接口中只有一個抽象方法的接口,稱為函數(shù)式接口,如:

/*** 函數(shù)式接口 */ public interface MyPredicte<T> {public boolean test(T t); }

可以使用注解 @FunctionalInterface 修飾的,則為函數(shù)式接口

/*** 接口,用于解決重復(fù)條件* @param <T>*/ @FunctionalInterface public interface MyPredicte<T> {public boolean test(T t); }

場景

對一個數(shù)進行某種運算

首先創(chuàng)建一個函數(shù)式接口

@FunctionalInterface public interface MyFun {public Integer getValue(Integer value); }

然后在定義一個方法,把方法作為參數(shù)傳遞

/*** 需求:對一個數(shù)進行運算*/public static void test5() {Integer value = operation(100, (x) -> x*x);System.out.println(value);}public static Integer operation(Integer num, MyFun myFun) {return myFun.getValue(num);}

訓(xùn)練

  • 調(diào)用Collections.sort()方法,通過定制排序比較兩個Employee(先比較年齡比,年齡相同比較姓名),使用Lambda表達式
public static void test() {List<Employee> employees = Arrays.asList(new Employee("張三", 18, 3333),new Employee("李四", 38, 55555),new Employee("王五", 50, 6666.66),new Employee("趙六", 16, 77777.77),new Employee("田七", 8, 8888.88));Collections.sort(employees, (e1, e2) -> {if(e1.getAge() == e2.getAge()) {return e1.getName().compareTo(e2.getName());} else {return Integer.compare(e1.getAge(), e2.getAge());}});employees.stream().map(Employee::getName).forEach(System.out::println);}

Java內(nèi)置函數(shù)接口

Comsumer 消費型接口

格式:Comsumer<T>

傳入?yún)?shù),然后對參數(shù)進行操作,沒有返回值

/*** 消費型接口*/public static void test() {happy(1000, (m) -> System.out.println("消費成功:" + m + "元"));}public static void happy(double money, Consumer<Double> consumer) {consumer.accept(money);}

Supplier 供給型接口

格式:Supplier<T>

T get();

傳入?yún)?shù),對參數(shù)進行操作,然后有返回值

/*** 供給型接口,供給功能如何實現(xiàn)*/public static void test2() {List<Integer> list = getNumList(10, () -> {Integer a = (int)(Math.random() * 10);return a;});list.stream().forEach(System.out::println);}/*** 產(chǎn)生指定個數(shù)的整數(shù)* @param n* @return*/public static List<Integer> getNumList(Integer n, Supplier<Integer> supplier) {List<Integer> list = new ArrayList<>();for (int i = 0; i < n; i++) {list.add(supplier.get());}return list;}

最后輸出結(jié)果

0 5 9 4 4 3 4 5 0 3

Function 函數(shù)型接口

格式:Function<T,R>

R apply(T t);

/*** 函數(shù)型接口* Function<T, R>*/public static void test3() {String str = strHandler("abcdefg", (x) -> {return x.toUpperCase().substring(0, 5);});System.out.println(str);}/*** 需求:用于處理字符串*/public static String strHandler(String str, Function<String, String> function) {// 使用apply方法進行處理,怎么處理需要具體實現(xiàn)return function.apply(str);}

輸出結(jié)果:

ABCDE

Predicate 斷言型接口

格式:Predicate<T>, 用于做一些判斷

/*** 斷言型接口(把長度大于3的str過濾出來)*/public static void test4() {List<String> list = Arrays.asList("abc", "abcd", "df", "cgg", "aaab");List<String> result = strPredict(list, (x) -> x.length() > 3);result.forEach(item -> {System.out.println(item);});}/*** 將滿足條件的字符串,放入到集合中*/public static List<String> strPredict(List<String> list, Predicate<String> predicate) {List<String> result = new ArrayList<>();list.forEach(item -> {if(predicate.test(item)) {result.add(item);}});return result;}

擴展

上述的四大核心接口,并不能被適用于一個特殊的應(yīng)用場景,只能滿足大部分的需求

因為他們對于參數(shù)的參入有局限性

同時后面針對這樣的情況,后面也使用子接口,進行了解決

作者:輕狂書生FS
原文鏈接:https://blog.csdn.net/LookForDream_/article/details/109157130

總結(jié)

以上是生活随笔為你收集整理的java compare 返回值_关于Java你不知道的那些事之Java8新特性[Lambda表达式和函数式接口]...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。