Flatmap example

Millies, Sebastian Sebastian.Millies at softwareag.com
Mon Dec 2 06:31:55 PST 2013

I can't get the second variant (without the inner class) to compile with b117.

The method reference in ".map(p::matcher)" seems illegal, and explicitly typing the Lambda-Parameter
"p" as "Pattern" leads to an Incompatible Types error. I have to cast the function that goes into the flatMap
to "Function<Pattern, Stream<String>>". Why is that?

-- Sebastian

-----Original Message-----
From: lambda-dev-bounces at openjdk.java.net [mailto:lambda-dev-bounces at openjdk.java.net] On Behalf Of Paul Sandoz
Sent: Monday, December 02, 2013 1:34 PM
Cc: lambda-dev at openjdk.java.net
Subject: Re: Flatmap example


You are using a raw type for the List of Pattern, which is making it difficult for the compiler to infer types. Try this:

        List<Pattern> patterns = Arrays.asList(Pattern.compile("Current.*?[/|]"),

                .flatMap(new Function<Pattern, Stream<String>>() {
                    public Stream<String> apply(Pattern p) {
                        return source.stream()

                .flatMap(p -> source.stream()

Previously i thought you wanted to do layered matching e.g. match a line with pattern P1, then match the result of that with pattern P2 and so on.

From your example i see that you wan to apply the pattern N times to the same source of lines to produce N different results (which may be getting into aspects of forked streams previously discussed). So i may have confused you with my suggestion of flatMap.

A potential problem with your current use of flatMap is that it has munged the results so that the association of the Pattern to matched result is now lost.


Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com

More information about the lambda-dev mailing list