Accumulating filtered values

mohan.radhakrishnan at polarisFT.com mohan.radhakrishnan at polarisFT.com
Tue Dec 3 03:37:49 PST 2013


Hi,
            I have two streams. One is the log line and another stream is 
the pattern list. I was trying to accumulate the result of 'm.find()' in a 
List because this function returns multiple values in a while loop. I 
think here instead of a Predicate I need some type of accumulator ? I 
searched but didn't find an example.

This code compiles and runs but I couldn't access the list of accumulated 
matches.

The result now is a set of Matchers.But not the matches. Each Matcher is 
printed two times.

[java.util.regex.Matcher[pattern=Current.*?[/|] region=0,447 lastmatch=]]
java.util.regex.Matcher[pattern=Current.*?[/|] region=0,447 lastmatch=]
[java.util.regex.Matcher[pattern=Current.*?[/|] region=0,447 lastmatch=]]
java.util.regex.Matcher[pattern=Current.*?[/|] region=0,447 lastmatch=]
[java.util.regex.Matcher[pattern=[0-9]+(/,|/|) region=0,447 lastmatch=]]
java.util.regex.Matcher[pattern=[0-9]+(/,|/|) region=0,447 lastmatch=]
[java.util.regex.Matcher[pattern=[0-9]+(/,|/|) region=0,447 lastmatch=]]
java.util.regex.Matcher[pattern=[0-9]+(/,|/|) region=0,447 lastmatch=]

Thanks.

        Predicate<Matcher> matches = m -> {
            List<String> list = new ArrayList();
            while(m.find()){
                String match = m.group();
                list.add(match);
            }
            return true;
        };

        //Style 1
        patterns.stream()
                .flatMap(new Function<Pattern, Stream<Matcher>>() {
                    @Override
                    public Stream<Matcher> apply(Pattern p) {
                        return source.stream()
                                .map(p::matcher)
                                .filter(matches).map(l -> {
                                    System.out.println( "[" + l + "]" );
                                    return l;
                                });
                    }
                })
                .forEach(System.out::println);


This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only.  If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit us at http://www.polarisFT.com


More information about the lambda-dev mailing list