1.行为参数化,就是一个方法接受多个不同的行为作为参数,并在内部使用它们,完成不同行为的能力
2.行为参数化可以让代码个好的适应不断变化的要求,减轻工作量
3.lambda表达式是这一应用更加简便
4.掌握分析谓词,定义出合适的接口和实现方法
经过数月的努力,纵横B2B V3.0正式发布。感谢所有用户的大力支持和耐心等待,升级过程中提出了众多有价值的功能建议,向他们致敬。本次升级直接跨越到3.0版本,意味着将会带来很多新的变化。首先最大的变化是用户中心,我们借鉴了大量SNS社区、WEB2.0网站的概念,增强了交互性和可操作性;其次彻底抛弃了关键词竞价模式,改为直接购买关键词,为网站带来直接收益创造了条件;对系统性能进行了大量改进,使得系
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 filterGreenApple(Listinventory){
Listresult=new ArrayList<>();
for (Apple apple : inventory) {
if ("green".equals(apple.getColor())) {
result.add(apple);
}
}
return result;
}
//从列表中根据参数筛选出绿色的苹果
public static ListfilerAppleByColor(ListappleList,String color){
List 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{
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 ListfilterApplePredicate(List appleList,ApplePredicate p){
List apples=new ArrayList<>();
for (Apple apple : appleList) {
if (p.test(apple)) {
apples.add(apple);
}
}
return apples;
}
public static ListfilterFannyApple(ListappleList,PredicateFormat p){
Listapples=new ArrayList<>();
for (Apple apple : appleList) {
apple.setSm(p.accept(apple));
apples.add(apple);
}
return apples;
}
//集成泛型接口的泛型类型方法
public static List filter(Listlist,AbstratPredicate pa){
Listlists=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 appleList=Arrays.asList(new Apple("yellow",150),new Apple("green",150),new Apple("green",100));
//过滤绿色的苹果
Listresult=filterGreenApple(appleList);
result.stream().forEach((Apple a)->System.out.println(a));
//根据颜色参数过滤苹果
ListcolorResult=filerAppleByColor(appleList,"yellow");
colorResult.stream().forEach(c->System.out.println(c));
//参数行为化多实现类写法,实现按重量和颜色挑选苹果
ListcolorWeightApple=filterApplePredicate(appleList,new filterGreenWeightApple() );
colorWeightApple.stream().forEach(cw->System.out.println(cw));
ListfannyApple=filterFannyApple(appleList,new filteFannyApple());
fannyApple.stream().forEach(f->System.out.println(f));
System.out.println("-----------------------------");
//参数行为化匿名类实现
Listniming=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---------");
ListlamApples=filterApplePredicate(appleList,(Apple a)->a.getWeight()>100);
lamApples.stream().forEach(la->System.out.println(la));
System.out.println("---------------");
ListlamApples1= 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));
//集成泛型接口的泛型类型方法
Listnums= Arrays.asList(1,2,3,4,5,6);
Listnumlist=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();
}










