Skip to content

Instantly share code, notes, and snippets.

@mstepien
Last active March 13, 2017 11:48
Show Gist options
  • Save mstepien/3b699c2ded148cb9e1cf088d24442a03 to your computer and use it in GitHub Desktop.
Save mstepien/3b699c2ded148cb9e1cf088d24442a03 to your computer and use it in GitHub Desktop.
Initialize a Java Map with lambda
/**
* quick and neat way to initialize a Map since Java 8
*/
Map<Integer, String> map = Collections.unmodifiableMap(
Stream.of(
new SimpleEntry<>(0, "zero"),
new SimpleEntry<>(1, "one"),
new SimpleEntry<>(2, "two"),
new SimpleEntry<>(3, "three")
).collect(Collectors.toMap(
(e) -> e.getKey(), (e) -> e.getValue()
)));
@mstepien
Copy link
Author

mstepien commented Mar 3, 2017

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