A brief discussion on the evolution of behavioral parameterization in JAVA8
1. Behavior parameterization is the ability of a method to accept multiple different behaviors as parameters and use them internally to complete different behaviors
2. Behavior parameterization allows the code to be personalized Adapt well to changing requirements and reduce workload
3. Lambda expressions make this application simpler
4. Master analytical predicates and define appropriate interfaces and implementation methods
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(); }
The above is the detailed content of A brief discussion on the evolution of behavioral parameterization in JAVA8. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.
