Skip to content

Instantly share code, notes, and snippets.

@ahlusar1989
Last active June 10, 2017 23:36
Show Gist options
  • Save ahlusar1989/358436a03da7214c23c97fbd67ed3449 to your computer and use it in GitHub Desktop.
Save ahlusar1989/358436a03da7214c23c97fbd67ed3449 to your computer and use it in GitHub Desktop.
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