1

I am new to Java8 syntax, How can we get the output as list after filtered. In my case filter returns an array.

Added in comments, is there any better way can we get it

Config config = new Config("ooo", "wc", "code");
    Config config1 = new Config("ppp", "wc", "code");
    Config config2 = new Config("ooo", "wc", "code");

    Config[] configs = {config, config1, config2};

    Config config4 = new Config("ooo", "REG", "code");
    Config config5 = new Config("ppp", "REG", "code");
    Config config6 = new Config("ooo", "REG", "code");

    Config[] _configs = {config4, config5, config6};

    PromoCode promoCode = new PromoCode(121, "VOUCHER", "121", configs);
    PromoCode promoCode1 = new PromoCode(122, "VOUCHER", "122", null);
    PromoCode promoCode2 = new PromoCode(123, "LINK", "123", configs);
    PromoCode promoCode3 = new PromoCode(124, "VOUCHER", "124", null);
    PromoCode promoCode4 = new PromoCode(125, "LINK", "125", _configs);
    PromoCode promoCode5 = new PromoCode(126, "LINK", "126", _configs);

    List<String> resultantValues = new ArrayList<String>();
    PromoCode[] promoCodes = {promoCode, promoCode1, promoCode2, promoCode3, promoCode4, promoCode5};
    Stream<PromoCode> stream = Stream.of(promoCodes);
    stream.parallel()
        .filter(x -> x.getCode().equalsIgnoreCase("VOUCHER"))
        .collect(Collectors.toList())
        .parallelStream()
        .forEach(x-> {
            Stream.of(x.getConfigs())
                .filter(t -> t.getOccasion().equals("wc"))
            //after filter, how can we get the output
             // List of list of strings format
                .forEach(o -> {
                    resultantValues.add(o.getProduct()+"_"+o.getProduct());
                });
        });

    System.out.println(resultantValues);

2 Answers 2

3

to retrieve a List<List<T>> it can be done as follows:

 Stream.of(promoCodes)
       .parallel() // is this really needed?
       .filter(x -> x.getCode().equalsIgnoreCase("VOUCHER"))          
       .map(x-> 
            Stream.of(x.getConfigs())
                .filter(t -> t.getOccasion().equals("wc"))
                .map(o ->  o.getProduct()+"_"+o.getProduct())
                .collect(Collectors.toList())
       )
       .collect(Collectors.toList());

or if you want a List<T> format then use flatMap:

 Stream.of(promoCodes)
       .parallel() // is this really needed?
       .filter(x -> x.getCode().equalsIgnoreCase("VOUCHER"))          
       .flatMap(x-> 
            Stream.of(x.getConfigs())
                  .filter(t -> t.getOccasion().equals("wc"))
                  .map(o ->  o.getProduct()+"_"+o.getProduct())
        )
        .collect(Collectors.toList());

or as @Holger mentions, for the second approach you can avoid the nesting in the flatMap with:

 Stream.of(promoCodes)
       .parallel() // is this really needed?
       .filter(x -> x.getCode().equalsIgnoreCase("VOUCHER"))
       .flatMap(x -> Arrays.stream(x.getConfigs()))
       .map(x -> x.getProduct() + "_" + x.getProduct())
       .collect(Collectors.toList());

Which is definitely more readable:


Note that I've also removed some of the unnecessary method calls such as the intermediate collecting to a list .collect(Collectors.toList()) , .parallelStream() et al.

Sign up to request clarification or add additional context in comments.

Comments

0

This should give you the desired results. First filter the promocodes with the given code VOUCHER. For each filtered promocode, you have an array of configs. We get that and flatten it to get a stream of Config objects. In the next step we filter out all the configs whose occasion is not equal to wc. Then we map all the matching config objects to get the desired result. At the final step we collect the result into a container.

final List<String> finalResult = Stream.of(promoCodes)
        .filter(pc -> pc.getCode().equalsIgnoreCase("VOUCHER"))
        .flatMap(pc -> Stream.of(pc.getConfigs()))
        .filter(conf -> conf.getOccasion().equals("wc"))
        .map(conf -> conf.getProduct() + "_" + conf.getProduct())
        .collect(Collectors.toList());

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.