Last active
June 10, 2017 23:36
-
-
Save ahlusar1989/358436a03da7214c23c97fbd67ed3449 to your computer and use it in GitHub Desktop.
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
import java.util.function.Function; | |
import java.util.*; | |
import java.util.stream.*; | |
static <X, Y, Z> Map<X, Z> transform(Map<? extends X, ? extends Y> input, | |
Function<Y, Z> function) { | |
return input.keySet().stream() | |
.collect(Collectors.toMap(Function.identity(), | |
key -> function.apply(input.get(key)))); | |
} | |
// Example Usage: | |
Map<String, String> input = new HashMap<String, String>(); | |
input.put("foo", "10"); | |
input.put("bar", "10"); | |
Map<String, Integer> output = transform(input, | |
(val) -> Integer.parseInt(val)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment