import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
/**
* Created by Administrator on 2017/8/19 0013.
*/
public class Test {
/*************************************JAVA8 in Action:行爲參數化,匿名類及lambda表達式的初步認知實例整理*****************************/
/**首先了解的幾個概念:
* 1.行爲參數化:就是一個方法接受多個不同的行爲作爲參數,並在內部是使用它們,完成不同行爲的能力,是一種可以幫助你處理頻繁的需求變更的一種軟件開發模式;
* 2.匿名類:與我們所熟悉的java局部類差不多,但是匿名類沒有名字,它允許你同事聲明並實例化一個類(隨用隨建);
* 3.ambda表達式:由參數,箭頭和主體組成,如:(Apple a1,Apple a2) -> a1.getWeight().compareTo(a2.getWeight());
* ----lambda參數------ -箭頭- -------------lambda主體---------------
* */
//我們以實現從一個列表中篩選出綠蘋果作爲例子:
//1.基礎數據
//創建蘋果實體類
class Apple{
private String color;
private double weight;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public Apple(String color, double weight) {
this.color = color;
this.weight = weight;
}
}
//2.由淺入深的實例:
//初級做法:僅僅只能用於選出綠色的蘋果
public static List<Apple> chooseGreenApple(List<Apple> apples){
List<Apple> result = new ArrayList<Apple>();//用於盛放篩選出來的綠蘋果的集合
for(Apple apple:apples){
if("green".equals(apple.getColor())){//選出綠蘋果放入到集合中
result.add(apple);
}
}
return result;
}
//一級拓展:以顏色作爲參數,可以根據參數選出想要的顏色的蘋果
public static List<Apple> choseAppleByColor(List<Apple> apples,String color){
List<Apple> result = new ArrayList<Apple>();//用於盛放篩選出來的綠蘋果的集合
for(Apple apple:apples){
if(color.equals(apple.getColor())){//根據參數選出所需要的蘋果放入到集合中
result.add(apple);
}
}
return result;
}
//二級拓展,對多個屬性進行篩選(如顏色,重量)
public static List<Apple> chooseApples(List<Apple> apples,String color, double weight,boolean flag){//謂詞flag用於區分根據顏色還是根據重量篩選
List<Apple> result = new ArrayList<Apple>();//用於盛放篩選出來的蘋果的集合
for(Apple apple:apples){
//根據flag確定使用以哪個參數爲依據來選出所需要的蘋果放入到集合中
if((flag && color.equals(apple.getColor())) || (!flag && apple.getWeight() > weight)){
result.add(apple);
}
}
return result;
}
//三級拓展,根據抽象條件進行篩選:
//定義一個接口來對選擇標準建模:
public interface ApplePredicate{
boolean test (Apple apple);
}
//以ApplePredicate的多個不同的實現來代表不同的選擇標準
//僅僅用來選出重的蘋果
public class AppleHeavy implements ApplePredicate{
public boolean test (Apple apple){
return apple.getWeight() > 180;
}
}
//如果僅僅用來選出綠色的蘋果
public class AppleColor implements ApplePredicate{
public boolean test (Apple apple){
return "green".equals(apple.getColor());
}
}
//在利用ApplePredicte改過之後,該方法就變成了這個樣子,
// 我們在使用的時候只需要創建不同個ApplePredicate對象,將他傳遞給chooseApples方法即可,大大的增加了他的靈活性
public static List<Apple> chooseApples(List<Apple> apples, ApplePredicate predicate){
List<Apple> result = new ArrayList<Apple>();//用於盛放篩選出來的蘋果的集合
for(Apple apple:apples){
if(predicate.test(apple)){
result.add(apple);
}
}
return result;
}
//終極超級酷炫拓展,將List類型抽象化
public interface predicatre<T>{
boolean test(T t);
}
public static <T> List<T> chooseSomeThind(List<T> list, Predicate<T> p){
List<T> result = new ArrayList<T>();
for(T e:list){
if(p.test(e)){
result.add(e);
}
}
return result;
}
@org.junit.Test
public void testChooseAppleByWhatYouWant(){
//創建集合:
List<Apple> appleList = Arrays.asList(new Apple("green",200),new Apple("red",150));
//初級做法:僅僅只能用於選出綠色的蘋果
List<Apple> greenApples__1 = chooseGreenApple(appleList);
//一級拓展:以顏色作爲參數,可以根據參數選出想要的顏色的蘋果
//例如篩選出紅蘋果:
List<Apple> greenApples__2 = choseAppleByColor(appleList,"red");
//二級拓展,對多個屬性進行篩選(如顏色,重量)
//例如篩選出紅蘋果:
List<Apple> greenApples__13 = chooseApples(appleList,"red",0,true);
//例如篩選出重蘋果:
List<Apple> weightApples__1 = chooseApples(appleList,"",180,false);
//三級拓展,根據抽象條件進行篩選:
//例如篩選出綠蘋果:
List<Apple> greenApples = chooseApples(appleList,new AppleColor());
//例如篩選出重蘋果:
List<Apple> weightApples_1 = chooseApples(appleList,new AppleHeavy());
//四級拓展,使用匿名類同時聲明和實例化一個類:(可以讓你無需事先實例化,隨用隨建,提高效率)
List<Apple> weightApples_2 = chooseApples(appleList, new ApplePredicate() {
public boolean test(Apple apple) {return apple.getWeight() > 180;}
});
//五級拓展,使用lambda表達式:(顯得更加乾淨整潔)
//選出綠色的蘋果
List<Apple> weightApples_3 = chooseApples(appleList,(Apple apple) -> "green".equals(apple.getColor()));
//終極超級酷炫拓展,將List類型抽象化:
//類型抽象化後,你可以廣泛的推廣了,可以用在西瓜上,汽車上,Integer,String。。。。。。。。。。。。
//例如:篩選出集合中包含“e”的單詞集合:
List<String> stringList = Arrays.asList("one","two","three","four");
List<String> include_e = chooseSomeThind(stringList,(String str)-> str.contains("e"));
//例如:篩選出集合中大於5的數字的集合:
List<Integer> integersList = Arrays.asList(1,2,3,4,5,6,7,8,10);
List<Integer> bigerThan_5 = chooseSomeThind(integersList,(Integer a)-> a>5);
System.out.print("非常完美!");
}
}
JAVA8 in Action:行爲參數化,匿名類及lambda表達式的初步認知實例整理
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.