Skip to content

Instantly share code, notes, and snippets.

@HoweChen
Last active April 3, 2020 08:54
Show Gist options
  • Save HoweChen/ae0a4c356d43f4e3fe32be7201284401 to your computer and use it in GitHub Desktop.
Save HoweChen/ae0a4c356d43f4e3fe32be7201284401 to your computer and use it in GitHub Desktop.
[两个list合并为一个map] #Java
public static <K, V> Map<K, V> zipToMap(List<K> keyList, List<V> valueList) throws Exception {
Map<K, V> result = new HashMap<>();
Iterator<K> keyIterator = keyList.iterator();
Iterator<V> valueIterator = valueList.iterator();
while (keyIterator.hasNext() && valueIterator.hasNext()) {
result.put(keyIterator.next(), valueIterator.next());
}
if (keyIterator.hasNext() || valueIterator.hasNext()) {
throw new Exception("The keyList and valueList size doesn't match.");
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment