JAVA8 in Action:行为参数化,匿名类及lambda表达式的初步认知实例整理
Posted shundong106
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA8 in Action:行为参数化,匿名类及lambda表达式的初步认知实例整理相关的知识,希望对你有一定的参考价值。
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("非常完美!"); } }
转自:https://blog.csdn.net/qq_37107280/article/details/77417500
以上是关于JAVA8 in Action:行为参数化,匿名类及lambda表达式的初步认知实例整理的主要内容,如果未能解决你的问题,请参考以下文章