Created
May 2, 2014 21:38
-
-
Save wffurr/b63e96721a6c69c725b8 to your computer and use it in GitHub Desktop.
Zip two maps into a map of K to Tuple<V1, Optional<V2>>
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
/** | |
* Combines two maps with the same key into a map of key to tuple(value 1, optional value 2) | |
*/ | |
public static <K, V1, V2> Map<K, Tuple<V1, Optional<V2>>> | |
zip(Map<K, V1> map1, Map<K, V2> map2) | |
{ | |
Map<K, Tuple<V1, Optional<V2>>> results = new HashMap<>(); | |
for (K key : map1.keySet()) | |
{ | |
V1 val1 = map1.get(key); | |
Optional<V2> val2 = Optional.fromNullable(map2.get(key)); | |
results.put(key, Tuple.from(val1, val2)); | |
} | |
return results; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment