Skip to content

Instantly share code, notes, and snippets.

@RichardWarburton
Created October 14, 2014 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardWarburton/3274e6e1bbd18a1cb79d to your computer and use it in GitHub Desktop.
Save RichardWarburton/3274e6e1bbd18a1cb79d to your computer and use it in GitHub Desktop.
Python Example
Python:
# Even numbers between 1 and 49:
[ x for x in range(1,50) if x % 2 == 0 ]
Java:
// Even numbers between 1 and 49:
IntStream.range(1, 50).filter(x -> x % 2 == 0).boxed().collect(toList());
@timyates
Copy link

Comprehension with java 8 and groovy-streams ;-)

import groovy.stream.Stream;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Comprehend {
    public static void main(String[] args) {
        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(entry -> entry.get("x") + entry.get("y"))
              .forEachRemaining(System.out::println);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment