Java匿名內(nèi)部類和Lambda(->) 的多種寫法總結(jié)
引入: 最近使用到 Arrays.sort(); 看了他的重載方法(試著模仿一下)
就以這個玩出了許多的方式;如下:自定義排序
首先 寫了個冒泡排序(備用)
//給一個integres 的數(shù)組, 然后再給個 Comparator的接口 c /** * * @param integers 給一個 integres 的數(shù)組,作為要排序的數(shù)組 * @param c 接受一個 Comparator 接口 ,然后使用Comparator 重寫的 compare 方法 */ public static void bubbleSort(Integer[] integers,Comparator c ){ int temp; for(int i = 0 ; i < integers.length-1;i++){ for(int j = 0 ; j < integers.length -1 -i;j++){ //判斷是從大到小還是從小到大 if(c.compare(integers[j] , integers[j+1]) > 0){ //慢 /*integers[j] += integers[j+1]; integers[j+1] = integers[j] - integers[j+1]; integers[j] = integers[j] - integers[j+1];*/ //更快 temp = integers[j]; integers[j] = integers[j+1]; integers[j+1] = temp; } } } }
認識幾種寫法
第一種寫法
//第一種寫法 匿名類寫法 bubbleSort(number, new Comparator() { @Override public int compare(Object o1, Object o2) { return (int)o1 - (int)o2; } });
第二種寫法(Lambda)
// 接受2個參數(shù)(數(shù)字),并返回他們的差值 // (x, y) -> x – y //第二種寫法 bubbleSort(number, (Object o1 ,Object o2)-> (int)o1 - (int)o2);
第三種寫法(Lambda)
/*第三種寫法 * 1.先把Object 轉(zhuǎn)為 int 然后執(zhí)行以下操作 * Comparator.comparingInt((Object o) -> (int) o); * * 2.會返回 (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); * 會讀取兩個對象 keyExtractor.applyAsInt(c1) , keyExtractor.applyAsInt(c2) * * 3.(Comparator.comparingInt((Object o) -> (int) o)))讀取一個(keyExtractor.applyAsInt(c1))后返回到這里(Comparator.comparingInt((Object o) -> (int) o))),然后再讀取一個(keyExtractor.applyAsInt(c2)) * * 4.兩個了之后就開始比較Integer.compare * public static int compare(int x, int y) { * return (x < y) ? -1 : ((x == y) ? 0 : 1); * } *5.最后經(jīng)過三元運算符后 返回對應(yīng)的數(shù) */ bubbleSort(number, Comparator.comparingInt((Object o) -> (int) o));
然后開始使用
import java.util.Arrays; import java.util.Comparator; public class Test { public static void main(String[] args) { Integer number[] = {1,4,2,23,32,43}; //第一種寫法 bubbleSort(number, new Comparator() { @Override public int compare(Object o1, Object o2) { return (int)o1 - (int)o2; } }); //第二種寫法 //1.先把Object 轉(zhuǎn)為 int 然后執(zhí)行以下操作 //Comparator.comparingInt((Object o) -> (int) o); //2.會進去 (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); //然后會讀取兩個對象keyExtractor.applyAsInt(c1) , keyExtractor.applyAsInt(c2) //3.Comparator.comparingInt((Object o) -> (int) o)) 讀取一個后返回到這里,然后再讀取一個 //4.兩個了之后就開始比較Integer.compare // public static int compare(int x, int y) { // return (x < y) ? -1 : ((x == y) ? 0 : 1); // } //5.返回 最后的數(shù) bubbleSort(number, Comparator.comparingInt((Object o) -> (int) o)); //第三種寫法 bubbleSort(number, (Object o1 ,Object o2)-> (int)o1 - (int)o2); System.out.println("排序后的數(shù)組"+Arrays.toString(number)); } //給一個integres 的數(shù)組, 然后再給個 Comparator的接口 c /** * * @param integers 給一個 integres 的數(shù)組,作為要排序的數(shù)組 * @param c 接受一個 Comparator 接口 ,然后使用Comparator 重寫的 compare 方法 */ public static void bubbleSort(Integer[] integers,Comparator c ){ int temp; for(int i = 0 ; i < integers.length-1;i++){ for(int j = 0 ; j < integers.length -1 -i;j++){ //判斷是從大到小還是從小到大 if(c.compare(integers[j] , integers[j+1]) > 0){ //慢 /*integers[j] += integers[j+1]; integers[j+1] = integers[j] - integers[j+1]; integers[j] = integers[j] - integers[j+1];*/ //更快 temp = integers[j]; integers[j] = integers[j+1]; integers[j+1] = temp; } } } } }
到此這篇關(guān)于Java匿名內(nèi)部類和Lambda(->) 的多種寫法總結(jié)的文章就介紹到這了,更多相關(guān)Java匿名內(nèi)部類 Lambda內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于web項目讀取classpath下面文件的心得分享
這篇文章主要介紹了關(guān)于web項目讀取classpath下面文件的心得,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07使用Jenkins自動化構(gòu)建工具進行敏捷開發(fā)
這篇文章主要為大家介紹了使用Jenkins自動化構(gòu)建工具進行敏捷開發(fā),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04