Flatmap example

mohan.radhakrishnan at polarisFT.com mohan.radhakrishnan at polarisFT.com
Mon Dec 2 01:12:12 PST 2013


Hi,
         Based on one of the suggestions by Paul Sandoz in the other 
thread "Basic functional style question" I coded this test. There are two 
unstructured lines of log that the code is trying to parse. Since I 
couldn't locate Java flatmap examples I looked at Scala code.

I first tried the commented code which showed the error "Incompatible 
parameter types in lambda". 

But the combination of anonymous class and lambda worked. Is this how this 
type of problem is solved by using flatmap ? All of this is very exciting 
:-)

Thanks,
Mohan

public class FlatMapTest {

    public static void main( String... argv ){

        List<String> source = new ArrayList<String>();
        source.add( "Peak Usage    : init:2359296, used:13914944, 
committed:13959168, max:50331648Current Usage : init:2359296, 
used:13913536, committed:13959168, max:50331648|------------------| 
committed:13.31Mb+---------------------------------------------------------------------+|//////////////////| 
                                                 | 
max:48Mb+---------------------------------------------------------------------+|------------------| 
used:13.27Mb");
        source.add( "Peak Usage    : init:2359296, used:13916608, 
committed:13959168, max:50331648Current Usage : init:2359296, 
used:13915200, committed:13959168, max:50331648|------------------| 
committed:13.31Mb+---------------------------------------------------------------------+|//////////////////| 
                                                 | 
max:48Mb+---------------------------------------------------------------------+|------------------| 
used:13.27Mb");

        List patterns = Arrays.asList(Pattern.compile("Current.*?[/|]"), 
Pattern.compile("[0-9]+(/,|/|)"));


        patterns.stream().flatMap(new Function<Pattern,Object>() {
            @Override
            public Object apply(Pattern p1) {
                return source.
                        stream().
                        map(p1::matcher).
                        filter(Matcher::find).map(matcher -> 
matcher.group());
            }
        }).forEach(x -> System.out.println(x));

//        patterns.stream().flatMap(( Pattern p1 ) -> source.
//                stream().
//                map(p1::matcher).
//                filter(Matcher::find).map(matcher -> 
matcher.group())).forEach(x -> System.out.println(x));
    }
}



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