如何使用lambda生成过滤Java 8中的嵌套流

时间:2016-03-02 12:10:51

标签: java lambda java-8

我正在尝试根据我的Deals选项过滤我的Deal但是当我尝试过滤代码时,我遇到类型不匹配错误Set<DealOptions>无法转换为布尔值。当所有交易选项都是红色时,我想保留交易。

这是我的代码:

import java.util.*;
import java.util.stream.Collectors;

class Deal {
    String dealname;
    String dealprice;
    Set<DealOptions> dealop;

    public String getDealname() {
        return dealname;
    }

    public void setDealname(String dealname) {
        this.dealname = dealname;
    }

    public String getDealprice() {
        return dealprice;
    }

    public void setDealprice(String dealprice) {
        this.dealprice = dealprice;
    }

    public Set<DealOptions> getDealop() {
        return dealop;
    }

    public void setDealop(Set<DealOptions> dealop) {
        this.dealop = dealop;
    }

}

class DealOptions {
    String optname;
    String color;

    public String getOptname() {
        return optname;
    }

    public void setOptname(String optname) {
        this.optname = optname;
    }

    public String getColor() {
        return color;
    }

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

}

public class Test {

    public static void main(String[] args) 
             {

        Deal s = new Deal();
        Set<DealOptions> ops = new HashSet<DealOptions>();
        DealOptions op = new DealOptions();
        s.setDealname("mongo");
        s.setDealprice("500");

        op = new DealOptions();
        op.setColor("red");
        op.setOptname("redop");

        ops.add(op);
        op = new DealOptions();
        op.setColor("blue");
        op.setOptname("blueop");

        ops.add(op);
        op = new DealOptions();
        op.setColor("green");
        op.setOptname("greenop");

        ops.add(op);

        s.setDealop(ops);

        List<Deal> dl = new ArrayList<Deal>();

        dl.add(s);
        ops = new HashSet<DealOptions>();
        s = new Deal();
        op = new DealOptions();
        s.setDealname("test2");
        s.setDealprice("200");

        op = new DealOptions();
        op.setColor("indigo");
        op.setOptname("indigop");

        ops.add(op);
        op = new DealOptions();
        op.setColor("violet");
        op.setOptname("violetop");

        ops.add(op);
        op = new DealOptions();
        op.setColor("orange");
        op.setOptname("orangeop");

        ops.add(op);

        s.setDealop(ops);

        dl.add(s);

        List<Deal> dev = dl.stream().filter(
                (p) -> p.getDealop().stream().filter((po) -> po.getColor().equals("red")).collect(Collectors.toSet()))
                .collect(Collectors.toList()); // error here

    }
}

错误:

  

无法从Set转换为boolean

如何纠正此错误如何根据我的交易选项过滤我的交易?

2 个答案:

答案 0 :(得分:7)

您可以使用allMatch(predicate)来确定某个选项的所有交易是否为红色:

  

返回此流的所有元素是否与提供的谓词匹配。

在这种情况下,谓词只是告诉选项是否为红色。

List<Deal> output =
    dl.stream()
      .filter(d -> d.getDealop().stream().allMatch(po -> po.getColor().equals("red")))
      .collect(Collectors.toList());

答案 1 :(得分:0)

public static Predicate<Deal> isFound(String color) {
    return p -> p.getDealop().stream().anyMatch(op->op.getColor().equals(color));
}


public static List<Deal> filterDeals (List<Deal> deals, Predicate<Deal> predicate){
        return deals.stream()
        .filter( predicate )
        .collect(Collectors.<Deal>toList());
}

//in main method add
List<Deal> dd = filterDeals(dl,isFound("red"));