2

I want to fetch all the deal options within my main deal whose colour matches with given values. How do I write a stream query to get only those deal options whose colour is equal to color in my values list?

When should I use anyMatch and why can't I use filters for inner streams?

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);

        ArrayList<String> values = new ArrayList<String>();
        values.add("red");
        values.add("blue");

        List<Deal> output
                = dl.stream()
                .filter(d -> d.getDealop().stream().anyMatch(po -> values.stream().anyMatch(v -> v.equals(po.getColor()))))
                .collect(Collectors.toList());

        System.out.println(output.size());

    }
}

1 Answer 1

4

In your case, you want to remove all the deal options from each deal where its color is not in a specified list of color. You can loop over your deals and use removeIf to remove deal options having an unwanted color.

Set<String> values = new HashSet<String>();
values.add("red");
values.add("blue");

List<Deal> output = 
        dl.stream()
          .map(d -> {
            d.getDealop().removeIf(o -> !values.contains(o.color));
            return d;
          })
          .collect(Collectors.toList());

or, in place:

dl.forEach(d -> d.getDealop().removeIf(o -> !values.contains(o.color)));
Sign up to request clarification or add additional context in comments.

1 Comment

Well, when the values are fixed (like in the "red" & "blue" example), every collection has “constant-time lookup”. Only the amount of that constant time may differ and for two values, a HashSet isn’t necessarily better than an ArrayList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.