java lamdba表达式

lamdba支持将代码块作为方法的参数,为只有一个抽象方法的接口创建实例,这种接口被称为函数式接口(functional interface)

命令模式的演化

package com.zrun;

import org.junit.Test;

public class TestCommand {
    /**
     * 命令模式的演化
     */
    @Test
    public void test() {
        System.out.println("一.以下是自己写Command实现类");
        ProcessArray pa = new ProcessArray();
        int[] intArr = { 1, 8 };
        pa.process(intArr, new PrintCommand());
        pa.process(intArr, new AddCommand());

        System.out.println("二.以下是匿名内部类实现方式");
        pa.process(intArr, new Command() {

            @Override
            public void process(int[] target) {
                for (int item : target)
                    System.out.println(item);
            }
        });
        pa.process(intArr, new Command() {

            @Override
            public void process(int[] target) {
                int cou = 0;
                for (int item : target) {
                    cou += item;
                }
                System.out.println(cou);
            }

        });

        System.out.println("三.以下是lamdba表达式实现方式");
        pa.process(intArr, target -> {
            for (int item : target)
                System.out.println(item);
        });
        pa.process(intArr, target -> {
            int cou = 0;
            for (int item : target) {
                cou += item;
            }
            System.out.println(cou);
        });
    }
}

interface Command {
    void process(int[] target);
}

class ProcessArray {
    public void process(int[] target, Command cmd) {
        cmd.process(target);
    }
}

class PrintCommand implements Command {

    @Override
    public void process(int[] target) {
        for (int item : target)
            System.out.println(item);
    }
}

class AddCommand implements Command {

    @Override
    public void process(int[] target) {
        int cou = 0;
        for (int item : target) {
            cou += item;
        }
        System.out.println(cou);
    }

}

多线程简化实现

Runnable接口就是函数式接口

package com.zrun;

import org.junit.Test;

public class TestThread {
    @Test
    public void test() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("我是匿名内部类实现的多线程");
            }
        }).start();

        new Thread(() -> {
            System.out.println("我是lamdba表达式实现的多线程");
        }).start();
    }
}

数组排序

package com.zrun;

import java.util.Arrays;
import java.util.Comparator;

import org.junit.Test;

public class TestComparator {
    @Test
    public void test() {
        String[] arr = { "ych", "hll", "wh" };
        // 使用匿名内部类实现数组排序
        Arrays.sort(arr, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        for (String s : arr)
            System.out.println(s);

        // 使用lamdba实现数组排序
        Arrays.sort(arr, (s1, s2) -> s2.compareTo(s1));
        for (String s : arr)
            System.out.println(s);
    }
}

转载于:https://www.cnblogs.com/yinchh/p/10473980.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值