浅谈JAVA8中行为参数化的演变过程
1.行为参数化,就是一个方法接受多个不同的行为作为参数,并在内部使用它们,完成不同行为的能力
2.行为参数化可以让代码个好的适应不断变化的要求,减轻工作量
3.lambda表达式是这一应用更加简便
4.掌握分析谓词,定义出合适的接口和实现方法
public static class Apple{ private String color; private Integer weight; private String sm; public String getSm() { return sm; } public void setSm(String sm) { this.sm = sm; } public Apple(String color, int weight) { this.color = color; this.weight = weight; } @Override public String toString() { return "Apple{" + "color='" + color + '\'' +", weight=" + weight +", sm='" + sm + '\'' +'}'; } 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; } } //从列表中筛选出绿色的苹果 public static List<Apple> filterGreenApple(List<Apple>inventory){ List<Apple>result=new ArrayList<>(); for (Apple apple : inventory) { if ("green".equals(apple.getColor())) { result.add(apple); } } return result; } //从列表中根据参数筛选出绿色的苹果 public static List<Apple>filerAppleByColor(List<Apple>appleList,String color){ List<Apple> apples=new ArrayList<>(); for (Apple apple : appleList) { if (apple.getColor().equals(color)) { apples.add(apple); } } return apples; } //统一定义行为参数接口类,这个行为的主体是apple public interface ApplePredicate{ boolean test(Apple apple); } public interface PredicateFormat{ String accept(Apple apple); } //定义泛型类的行为参数接口类,这个行为的主体不在局限某一个实物 public interface AbstratPredicate<T>{ boolean test(T t); } //参数行为化多实现类写法,实现按重量和颜色挑选苹果 public static class filterGreenWeightApple implements ApplePredicate{ @Override public boolean test(Apple apple) { return apple.getColor().equals("green")&&apple.getWeight()>100; } } public static class filteFannyApple implements PredicateFormat{ @Override public String accept(Apple apple) { String ss= apple.getWeight()>100? "light":"heavy"; return "A"+ ss+apple.getColor()+"Apple"; } } public static List<Apple>filterApplePredicate(List<Apple> appleList,ApplePredicate p){ List<Apple> apples=new ArrayList<>(); for (Apple apple : appleList) { if (p.test(apple)) { apples.add(apple); } } return apples; } public static List<Apple>filterFannyApple(List<Apple>appleList,PredicateFormat p){ List<Apple>apples=new ArrayList<>(); for (Apple apple : appleList) { apple.setSm(p.accept(apple)); apples.add(apple); } return apples; } //集成泛型接口的泛型类型方法 public static <T> List<T> filter(List<T>list,AbstratPredicate<T> pa){ List<T>lists=new ArrayList<>(); for (T t : list) { if (pa.test(t)) { lists.add(t); } } return lists; } //匿名类的笨重感 public static class MeaningOfThis{ public final int value=4; public void doIt(){ int value=6; Runnable r=new Runnable() { public final int value=5; @Override public void run() { int value=10; System.out.println(this.value); } }; r.run(); } } public static void main(String[] args) { List<Apple> appleList=Arrays.asList(new Apple("yellow",150),new Apple("green",150),new Apple("green",100)); //过滤绿色的苹果 List<Apple>result=filterGreenApple(appleList); result.stream().forEach((Apple a)->System.out.println(a)); //根据颜色参数过滤苹果 List<Apple>colorResult=filerAppleByColor(appleList,"yellow"); colorResult.stream().forEach(c->System.out.println(c)); //参数行为化多实现类写法,实现按重量和颜色挑选苹果 List<Apple>colorWeightApple=filterApplePredicate(appleList,new filterGreenWeightApple() ); colorWeightApple.stream().forEach(cw->System.out.println(cw)); List<Apple>fannyApple=filterFannyApple(appleList,new filteFannyApple()); fannyApple.stream().forEach(f->System.out.println(f)); System.out.println("-----------------------------"); //参数行为化匿名类实现 List<Apple>niming=filterApplePredicate(appleList, new ApplePredicate() { @Override public boolean test(Apple apple) { return apple.getColor().equals("green"); } }); niming.stream().forEach(n->System.out.println(n)); //匿名类的笨重感 MeaningOfThis mo=new MeaningOfThis(); mo.doIt(); //lambda表达式改写 System.out.println("-------我是lambda---------"); List<Apple>lamApples=filterApplePredicate(appleList,(Apple a)->a.getWeight()>100); lamApples.stream().forEach(la->System.out.println(la)); System.out.println("---------------"); List<Apple>lamApples1= filterFannyApple(appleList,(Apple a)->{ String ss=a.getWeight()>100?"lighter":"heavyer"; return "A"+ss+a.getColor()+"Apple"; }); lamApples1.sort((Apple a,Apple a1)->{ if (!a1.getWeight().equals(a.getWeight())) { return a1.getWeight().compareTo(a.getWeight()); }else { return a1.getColor().compareTo(a.getColor()); } }); lamApples1.stream().forEach(la1->System.out.println(la1)); //集成泛型接口的泛型类型方法 List<Integer>nums= Arrays.asList(1,2,3,4,5,6); List<Integer>numlist=filter(nums,(Integer i)->i%2==0); numlist.sort((Integer a,Integer a1)->a.compareTo(a1)); numlist.stream().forEach(i->System.out.println(i)); //自带的排序行为参数化的排序 Thread t=new Thread(()->System.out.println(new Apple("red",100))); t.start(); }
以上是浅谈JAVA8中行为参数化的演变过程的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
