Skip to content

Instantly share code, notes, and snippets.

@wffurr
Created May 2, 2014 21:38
Show Gist options
  • Save wffurr/b63e96721a6c69c725b8 to your computer and use it in GitHub Desktop.
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>>
/**
* 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