Skip to content

Instantly share code, notes, and snippets.

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 hertzsprung/2968389 to your computer and use it in GitHub Desktop.
Save hertzsprung/2968389 to your computer and use it in GitHub Desktop.
Functional computation on a collection
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Dave");
names
.mapped(e -> { return e.length(); })
.asIterable() // returns an Iterable of BiValue elements
// an element's key is the person's name, its value is the string length
.filter(e -> e.getValue() >= 4)
.sorted((a, b) -> a.getValue() - b.getValue())
.forEach(e -> { System.out.println(e.getKey() + '\t' + e.getValue()); });
@aruld
Copy link

aruld commented Sep 15, 2012

Updated version of this code using the recent APIs:

        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Dave", "Ed");
        names.stream()
                .mapped(name -> name.length())
                .filterValues((length) -> length >= 4)
                .sorted((name1, name2) -> name1.length() - name2.length())
                .forEach((name, length) -> System.out.println(name + '\t' + length));

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