Last active
April 3, 2020 08:54
-
-
Save HoweChen/ae0a4c356d43f4e3fe32be7201284401 to your computer and use it in GitHub Desktop.
[两个list合并为一个map] #Java
This file contains 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
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