How to filter nested streams in Java 8 with lambda productions

10,160

You can use allMatch(predicate) to determine if all deals of an option are red:

Returns whether all elements of this stream match the provided predicate.

In this case, the predicate simply tells whether an option is red or not.

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

Related videos on Youtube

Cœur
Author by

Cœur

Everybody should contribute to clean up Stack Overflow. SO is intended to be a top-quality Q&amp;A site, meant not just for the OP, but for posterity. Thanks to search engines, questions and answers become authoritative for the whole Internet. --Paul Draper TODO: disambiguate the 18,300+ duplicate titles from 41,600+ questions fix the uneditable titles (1,117 titles with length &lt; 15) fix the uneditable titles (containing "help", "problem", "question", "doubt", …) fix the uneditable messages (containing "mydomain.com", "domain.com", "mysite.com", "site.com", "abc.com", "xyz.com", …) fix the uneditable messages with link shorteners (5,032 url:goo.gl, 3,673 url:bit.ly, 1,982 url:tinyurl.com, 1,748 url:cl.ly, …) remove the dead images/codes (8,051 url:imageshack.us, 2,818 url:pastie.org, 2,307 url:photobucket, 430 url:skitch.com, 214 url:rapidshare.com, 78 url:paste.ofcode.org, 58 url:expirebox.com, 4 url:megaupload.com, …) fix the broken links in messages, broken links to connect.microsoft.com, … review the potentially broken Apple links: #DOCUMENTATION in the URL, /library but not /archive in the URL, url:developer.apple.com/mac/library, url:developer.apple.com/safari/library rollback the 99+ solved, resolved, fixed, answered titles (meta, alternative query) correct the spelling in titles correct the 6,600+ "thanks in advanced" and 1,100+ "thanks in advice", …

Updated on September 15, 2022

Comments

  • Cœur
    Cœur over 1 year

    I am trying to filter my Deals based on my Deal options but when I try to filter the code I get a type mismatch error that Set<DealOptions> cannot be converted to boolean. I want to keep the deals when all the deal options are red.

    Here is my code:

    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
    
        }
    }
    

    Error:

    Cannot convert from Set to boolean

    How do rectify this error how can I filter my deals based on my deal options?

  • Mam's
    Mam's over 5 years
    List<Deal> dd1 = dl.stream().filter(d->d.getDealop().stream().anyMatch(p->p.g‌​etColor().equals("re‌​d"))) .collect(Collectors.toList());
  • Ram
    Ram over 4 years
    I think this list would return empty, if the list contains any other color rather than "red" color. cause it would return false as "Returns whether all elements of this stream match the provided predicate." anyMatch returns all in this case.