Last active
March 13, 2017 11:48
-
-
Save mstepien/3b699c2ded148cb9e1cf088d24442a03 to your computer and use it in GitHub Desktop.
Initialize a Java Map with lambda
This file contains hidden or 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
/** | |
* 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() | |
))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Embedded at blog post http://www.smart.biz.pl/techblog/code-for-maintainability-functional-programming-in-java