Created
March 11, 2014 19:46
-
-
Save timyates/9493588 to your computer and use it in GitHub Desktop.
Blog post for Java groovy-stream integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Stream.from( x:[1,2,3], y:[4,5,6] ) | |
.map { x + y } | |
.filter { it % 2 } | |
.each { println it } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String,List<Integer>> map = new HashMap<String,List<Integer>>() {{ | |
put( "x", Arrays.asList( 1, 2, 3 ) ) ; | |
put( "y", Arrays.asList( 4, 5, 6 ) ) ; | |
}} ; | |
Stream<Integer> s = Stream.from( map ) | |
.map( new Function<Map<String, Integer>, Integer>() { | |
@Override | |
public Integer call( Map<String, Integer> map ) { | |
return map.get( "x" ) + map.get( "y" ); | |
} | |
} ) | |
.filter( new Predicate<Integer>() { | |
@Override | |
public boolean call( Integer val ) { | |
return val % 2 == 1; | |
} | |
} ) ; | |
while( s.hasNext() ) { | |
System.out.println( s.next() ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Map<String,List<Integer>> map = new HashMap<String,List<Integer>>() {{ | |
put( "x", Arrays.asList( 1, 2, 3 ) ) ; | |
put( "y", Arrays.asList( 4, 5, 6 ) ) ; | |
}} ; | |
Stream.from( map ) | |
.map( e -> e.get( "x" ) + e.get( "y" ) ) | |
.filter( e -> e % 2 == 1 ) | |
.forEachRemaining( System.out::println ) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment