Java 匿名内部类 和 Lambda(->) 的多种写法

引入: 最近使用到 Arrays.sort(); 看了他的重载方法(试着模仿一下)
方法就以这个玩出了许多的方式;如下:自定义排序

首先 写了个冒泡排序(备用)

		//给一个integres 的数组, 然后再给个 Comparator的接口 c
	/**
     *
     * @param  integers 给一个 integres 的数组,作为要排序的数组
     * @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个参数(数字),并返回他们的差值  
		// (x, y) -> x – y  
		//第二种写法
        bubbleSort(number, (Object o1 ,Object o2)-> (int)o1 - (int)o2);

第三种写法(Lambda)

	    /*第三种写法
        * 1.先把Object 转为 int 然后执行以下操作
        * 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.最后经过三元运算符后 返回对应的数
        */
        
        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 转为 int 然后执行以下操作
        //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.返回 最后的数
        bubbleSort(number, Comparator.comparingInt((Object o) -> (int) o));

		//第三种写法
        bubbleSort(number, (Object o1 ,Object o2)-> (int)o1 - (int)o2);


        System.out.println("排序后的数组"+Arrays.toString(number));
        
   		 }
   		 

   //给一个integres 的数组, 然后再给个 Comparator的接口 c
	/**
     *
     * @param  integers 给一个 integres 的数组,作为要排序的数组
     * @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;

                }
            }
        }


   	 }
    }

okok 没了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Conan_Ritchie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值