Java8
作者:互联网
1.通过行为参数化传递代码
行为参数化,就是一个方法接受多个不同的行为作为参数,并在内部使用它们,完成不同行为的能力。
例如
package com.anson.demo.entity;
public class Apple {
private String color;
private Integer weight;
public Apple() {
}
public Apple(String color, Integer weight) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple{" +
"color='" + color + '\'' +
", weight=" + weight +
'}';
}
}
没使用Lambda表达式
package com.anson.demo.test;
import com.anson.demo.entity.Apple;
public interface AppleFormatter {
String accept(Apple a);
}
package com.anson.demo.test;
import com.anson.demo.entity.Apple;
public class AppleFancyFormatter implements AppleFormatter {
@Override
public String accept(Apple a) {
String characteristic = a.getWeight() >150 ? "heavy" : "light";
return "A" + characteristic + "" +a.getColor() + "Apple";
}
}
package com.anson.demo.test;
import com.anson.demo.entity.Apple;
public class AppleSimpleFormatter implements AppleFormatter {
@Override
public String accept(Apple a) {
return "An apple of" + a.getWeight() + "g";
}
}
package com.anson.demo.test;
import com.anson.demo.entity.Apple;
import java.util.ArrayList;
import java.util.List;
public class AppleTest {
public static void main(String[] args) {
List<Apple> apples = new ArrayList<>();
apples.add(new Apple("红色",120));
apples.add(new Apple("红色",160));
apples.add(new Apple("红色",100));
apples.add(new Apple("绿色",120));
apples.add(new Apple("绿色",180));
prettyPrintApples(apples,new AppleFancyFormatter());
prettyPrintApples(apples,new AppleSimpleFormatter());
//prettyPrintApples(apples,(Apple a) -> "An apple of" + a.getWeight() + "g");
}
public static void prettyPrintApples(List<Apple> apples , AppleFormatter appleFormatter){
for (Apple apple : apples) {
String outPrint = appleFormatter.accept(apple);
System.out.println(outPrint);
}
}
}
使用Lambda表达式
package com.anson.demo.test;
public interface Predicate<T> {
boolean test(T t);
}
package com.anson.demo.test;
import com.anson.demo.entity.Apple;
import java.util.ArrayList;
import java.util.List;
public class AppleTest {
public static void main(String[] args) {
List<Apple> apples = new ArrayList<>();
apples.add(new Apple("红色",120));
apples.add(new Apple("红色",160));
apples.add(new Apple("红色",100));
apples.add(new Apple("绿色",120));
apples.add(new Apple("绿色",180));
List<Apple> appleFiter = fiter(apples, (Apple a) -> "红色".equals(a.getColor()));
for (Apple apple : appleFiter) {
System.out.println(apple);
}
}
public static <T> List<T> fiter(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<>();
for (T e: list) {
if(p.test(e)){
result.add(e);
}
}
return result;
}
}
2.Lambda 表达式
2.1
语法:
(parameters) -> expression 或 (parameters) -> { statements; }
参数列表——这里它采用了Comparator中compare方法的参数,两个Apple。
箭头——箭头->把参数列表与Lambda主体分隔开。
Lambda主体——比较两个Apple的重量。表达式就是Lambda的返回值了。
例如
2.2
1)函数式接口就是只定义一个抽象方法的接口。
例如
注意 接口现在还可以拥有默认方法(即在类没有对方法进行实现时,其主体为方法提供默认实现的方法)。哪怕有很多默认方法,只要接口只定义了一个抽象方法,它就仍然是一个函数式接口。
2)用函数式接口可以干什么呢?
Lambda表达式允许你直接以内联的形式为函数式接口的抽象方法提供实现,并把整个表达式作为函数式接口的实例例(具体说来,是函数式接口一个具体实现 的实例)。
2.3
函数描述符
函数式接口的抽象方法的签名基本上就是Lambda表达式的签名。我们将这种抽象方法叫作函数描述符。
标签:Apple,Java8,anson,apples,new,public,String 来源: https://blog.csdn.net/Anson619/article/details/113944675