Java8新特性
Project 01
- 給出了行為參數(shù)化傳遞代碼的方式
- 定義一個標(biāo)準(zhǔn)的謂詞模板
public interface ApplePredicate{
boolean test (Apple apple);
}
- 用ApplePredicate的多個實(shí)現(xiàn)代表不同的選擇標(biāo)準(zhǔn)
public class AppleHeavyWeightPredicate implements ApplePredicate{
public boolean test(Apple apple){
return apple.getWeight() > 150;
}
}
public class AppleGreenColorPredicate implements ApplePredicate{
public boolean test(Apple apple){
return "green".equals(apple.getColor());
}
}
- 設(shè)置抽象條件篩選
public static List<Apple> filterApples(List<Apple> inventory,ApplePredicate p){
List<Apple> result = new ArrayList<>();
for(Apple apple: inventory){
if(p.test(apple)){
result.add(apple);
}
}
return result;
}
- 代碼傳遞行為
public class AppleRedAndHeavyPredicate implements ApplePredicate{
public boolean test(Apple apple){
return "red".equals(apple.getColor()) && apple.getWeight() > 150;
}
}
List<Apple> redAndHeavyApples = filterApples(inventory, new AppleRedAndHeavyPredicate());
- 使用匿名類
List<Apple> redApples = filterApples(inventory, new ApplePredicate() {
public boolean test(Apple apple){
return "red".equals(apple.getColor());
});
}
- 使用Lambda表達(dá)式
List<Apple> result = filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()));
- 將List抽象化
public interface Predicate<T>{
boolean test(T t);
}
public static <T> List<T> filter(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<>();
for(T e: list){
if(p.test(e)){
result.add(e);
}
}
return result;
}
Project 02
匿名類與Lambda表達(dá)式的使用
使用函數(shù)式接口
- java.util.function.Predicate<T>接口定義了一個名叫test的抽象方法,它接受泛型 T對象列肢,并返回一個boolean
@FunctionalInterface
public interface Predicate<T>{
boolean test(T t);
}
public static <T> List<T> filter(List<T> list, Predicate<T> p) {
List<T> results = new ArrayList<>();
for(T s: list){
if(p.test(s)){
results.add(s);
}
}
return results;
}
Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty(); List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
- java.util.function.Consumer<T>定義了一個名叫accept的抽象方法溉委,它接受泛型T的對象秸妥,沒有返回(void)。你如果需要訪問類型T的對象,并對其執(zhí)行某些操作挥等,就可以使用這個接口。
@FunctionalInterface
public interface Consumer<T>{
void accept(T t);
}
public static <T> void forEach(List<T> list, Consumer<T> c){
for(T i: list){
c.accept(i);
}
}
forEach(Arrays.asList(1,2,3,4,5), (Integer i) -> System.out.println(i));
- java.util.function.Function<T, R>接口定義了一個叫作apply的方法堤尾,它接受一個泛型T的對象肝劲,并返回一個泛型R的對象。如果你需要定義一個Lambda,將輸入對象的信息映射到輸出辞槐,就可以使用這個接口掷漱。
@FunctionalInterface
public interface Function<T, R>{
R apply(T t);
}
public static <T, R> List<R> map(List<T> list, Function<T, R> f) {
List<R> result = new ArrayList<>();
for(T s: list){
result.add(f.apply(s));
}
return result;
}
List<Integer> l = map(Arrays.asList("lambdas","in","action"), (String s) -> s.length());
Project 03
- 引入流Stream
List<Apple> heavyApples = inventory.stream().filter((Apple a) -> a.getWeight() > 150).collect(toList());
- 如何使用Stream
- 如何用流收集數(shù)據(jù)
- 并行數(shù)據(jù)處理
List<Apple> heavyApples = inventory.parallelStream().filter((Apple a) -> a.getWeight() > 150) .collect(toList());
Project 04
- 使用Java8新特性重構(gòu)代碼
- Optional
- CompletableFuture