借物表
在此文章记录了: 😍Weidows-の成长路线#lambda
简单讲解
Lambda 是函数话编程,Java8 引入的特性,有广泛使用.
主要是用作解决匿名内部类 overwrite
的问题,替代可函数化的 interface
作为参数进行传递
快速上手
进一步
public class Lambda { public static void main(String[] args) { Lambda tester = new Lambda();
MathOperation addition = (int a, int b) -> a + b;
MathOperation subtraction = (a, b) -> a - b;
MathOperation multiplication = (int a, int b) -> { return a * b; };
MathOperation division = (int a, int b) -> a / b;
System.out.println("10 + 5 = " + tester.operate(10, 5, addition)); System.out.println("10 - 5 = " + tester.operate(10, 5, subtraction)); System.out.println("10 x 5 = " + tester.operate(10, 5, multiplication)); System.out.println("10 / 5 = " + tester.operate(10, 5, division));
GreetingService greetService1 = message -> System.out.println("Hello " + message);
GreetingService greetService2 = (message) -> System.out.println("Hello " + message);
greetService1.sayMessage("Runoob"); greetService2.sayMessage("Google");
}
private int operate(int a, int b, MathOperation mathOperation) { return mathOperation.operation(a, b); }
}
interface MathOperation { int operation(int a, int b); }
interface GreetingService { void sayMessage(String message);
}
|
作用域
public class Lambda2 { public static void main(String args[]) {
final int num = 1;
Converter<Integer, String> s = (param) -> System.out.println(String.valueOf(param + num));
s.convert(2); }
public interface Converter<T1, T2> { void convert(int i); } }
|
forEach
public class ForEach { public static void main(String[] args) { List<String> list = new ArrayList<>(Arrays.asList("I", "love", "you", "too"));
for (String str : list) { if (str.length() > 3) System.out.println(str); }
list.forEach(new Consumer<String>() { @Override public void accept(String t) { if (t.length() > 3) System.out.println(t); } });
list.forEach(s -> { if (s.length() > 3) System.out.println(s); }); } }
|
多线程
创建新类
- 复杂,对于像是 Runnable 这种函数化接口,这样实现起来麻烦
public class TestYield { public static void main(String[] args) { MyYield myYield = new MyYield();
new Thread(myYield, "a").start(); new Thread(myYield, "b").start(); } }
class MyYield implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName() + "线程开始执行"); Thread.yield(); System.out.println(Thread.currentThread().getName() + "线程停止执行"); } }
|
匿名内部类
public class TestYield { public static void main(String[] args) { Runnable myYield = new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName() + "线程开始执行"); Thread.yield(); System.out.println(Thread.currentThread().getName() + "线程停止执行"); } };
new Thread(myYield, "a").start(); new Thread(myYield, "b").start(); } }
|
Lambda
- 在会用 Lambda 情况下,代码可读性和简洁度大于匿名内部类
public class TestYield { public static void main(String[] args) { Runnable myYield = () -> { System.out.println(Thread.currentThread().getName() + "线程开始执行"); Thread.yield(); System.out.println(Thread.currentThread().getName() + "线程停止执行"); };
new Thread(myYield, "a").start(); new Thread(myYield, "b").start(); } }
|