java8 in action:第一章学习,走进谓词(predicate)

好久都没有更新简书了,最近开始仔细学习java8。我属于那种不是特别聪明的程序员,一般都是买书,敲代码,看源码。很笨的学习方式。今天看了《java 8 in action》第一章,也顺便敲了敲代码。顺便分享一下其中的坑。

先上一个简单的demo。实体类Apple:

public  class Apple {

 private int weight = 0;
 private String color = "";

 public Apple(int weight, String color){
     this.weight = weight;
     this.color = color;
 }

 public Integer getWeight() {
     return weight;
 }

 public void setWeight(Integer weight) {
     this.weight = weight;
 }

 public String getColor() {
     return color;
 }

 public void setColor(String color) {
     this.color = color;
 }

 public String toString() {
     return "Apple{" +
            "color='" + color + '\'' +
            ", weight=" + weight +
            '}';
 }

 }

通过list集合找出颜色为red的苹果。

public class Test1 {

public static void main(String[] args) {
    
    List<Apple> inventory=Arrays.asList(new Apple(10,"red"),
            new Apple(100,"green"),
            new Apple(200,"red"));
    List<Apple>  redApples=filterApples(inventory,Test1::isRedApple);
    System.out.println(redApples);

}

public static List<Apple> filterApples(List<Apple> inventory,Predicate<Apple> p){
    List<Apple> result=new ArrayList<Apple>();
    for(Apple apple:inventory){
        if(p.test(apple)){
            result.add(apple);
        }
    }
    return result;
}

public static boolean isRedApple(Apple apple){
    return "red".equals(apple.getColor());
}
}

注意代码:

redApples=filterApples(inventory,Test1::isRedApple);

::表示方法。Test1::isRedApple就表示在Test1类下面的方法。
比如要查询重量大于100g的苹果:
先写判断的方法:

  public static boolean isHeavyApple(Apple apple){
    return apple.getWeight()>100;
}   

再用predicate中去做筛选:

public static List<Apple> filterHeavyApples(List<Apple> inventory,Predicate<Apple> p){
    List<Apple> result=new ArrayList<Apple>();
    for(Apple apple:inventory){
        if(apple.getWeight()>100){
            result.add(apple);
        }
    }
    return result;
}

这样就可以看到结果了:

List<Apple>  heavyApples=filterApples(inventory,Test1::isHeavyApple);
    System.out.println("大于100g的苹果:"+heavyApples);

好了接下来看下一下predicate的源码。

  • the type of the input to the predicate
  • **public interface Predicate<T> **

predicate的参数为泛型。他的方法其实不多:

  • boolean test(T t);
  • default Predicate<T> and(Predicate<? super T> other)
  • default Predicate<T> negate()
  • default Predicate<T> or(Predicate<? super T> other)
  • static <T> Predicate<T> isEqual(Object targetRef)

最常用的就是test这个方法。
default Predicate<T> and(Predicate<? super T> other) 返回的正常的return (t) -> test(t) && other.test(t);

而认识negate这个单词就知道了:否定的意思,自然返回:
return (t) -> !test(t);

最后两个方法注意关键字or,isEqual就好。官方api的解释也不多。看看就知道什么意思了。

这里再将大于150g的苹果使用stream()的方式简写,使用lambda表达式:

//顺序处理:
    List<Apple> heavyApples2=inventory.stream().filter((Apple a) -> a.getWeight()>150)
            .collect(toList());
    
    System.out.println("大于100g的苹果01"+heavyApples2);
    
    //并行处理
    List<Apple> heavyApples3=inventory.parallelStream().filter((Apple a) -> a.getWeight()>150)
            .collect(toList());
    
    System.out.println("大于100g的苹果02"+heavyApples2);

好了,今天就到这里了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容