码迷,mamicode.com
首页 > 编程语言 > 详细

java8实战:filter的简单使用

时间:2019-01-06 12:42:24      阅读:950      评论:0      收藏:0      [点我收藏+]

标签:分享   height   图片   array   bsp   技术   system   OLE   接口   

《JAVA8实战》中的例子

要实现的功能:通过Apple的color或weight属性,对List<Apple>进行筛选。

1、首先定义com.owl.entity.Apple:

package com.owl.entity;

public
class Apple { private String color; private Integer weight; 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; } }

 

2、生成一个简单的List<Apple>集合

package com.owl.app;

import java.util.ArrayList;
import java.util.List;

import com.owl.entity.Apple;

public class demo {
    public static void main(String[] args) {
        List<Apple> appleList = new ArrayList<Apple>();
        
        Apple redApple = new Apple();
        redApple.setColor("red");
        redApple.setWeight(180);
        appleList.add(redApple);
        
        Apple greenApple = new Apple();
        greenApple.setColor("green");
        greenApple.setWeight(120);
        appleList.add(greenApple);
    }
}

3、在com.owl.entity.Apple中定义筛选条件(绿苹果或者重量大于150的苹果)

    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

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

4、在com.owl.app.demo中定义接口:

    public interface Predicate<T> {
        boolean test(T t);
    }

5、在com.owl.app.demo中定义filter方法:

    static List<Apple> AppleFilter(List<Apple> apples, Predicate<Apple> p) {
        List<Apple> resultApples = new ArrayList<Apple>();
        for (Apple apple:apples) {
            if(p.test(apple)) {
                resultApples.add(apple);
            }
        }
        return resultApples;
    }

6、在main函数中使用filter筛选苹果

List<Apple> greenAppleSet = AppleFilter(appleList, Apple::isGreenApple);
List<Apple> heavyAppleSet = AppleFilter(appleList, Apple::isHeavyApple);
        
System.out.println("=======绿苹果=======");
for (Apple apple:greenAppleSet) {
     System.out.println(apple.getColor());
}
        
System.out.println("=======大苹果=======");
for (Apple apple:heavyAppleSet) {
     System.out.println(apple.getWeight());
}

 

结果:

技术分享图片

 

为了实现上述功能,除了需要定义筛选条件之外,仍需要定义Predicate<T>和AppleFilter方法未免太过麻烦,通过lambda表达式有更简单的写法:

List<Apple> greenAppleSet = appleList.stream().filter((Apple apple)->apple.getColor().equals("green")).collect(Collectors.toList());
List<Apple> heavyAppleSet = appleList.stream().filter((Apple apple)->apple.getWeight()>150).collect(Collectors.toList());

System.out.println("=======绿苹果=======");
for (Apple apple:greenAppleSet) {
System.out.println(apple.getColor());
}

System.out.println("=======大苹果=======");
for (Apple apple:heavyAppleSet) {
System.out.println(apple.getWeight());
}

 

java8实战:filter的简单使用

标签:分享   height   图片   array   bsp   技术   system   OLE   接口   

原文地址:https://www.cnblogs.com/wsfu/p/10228272.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!