public static < K , V > Map < K , V > zipMap ( Iterable < K > keys , Iterable < V > values ) { Map < K , V > retVal = new LinkedHashMap <> ( ) ; Iterator < K > keysIter = keys . iterator ( ) ; Iterator < V > valsIter = values . iterator ( ) ; while ( keysIter . hasNext ( ) ) { final K key = keysIter . next ( ) ; Preconditions . checkArgument ( valsIter . hasNext ( ) , "number of values[%s] less than number of keys, broke on key[%s]" , retVal . size ( ) , key ) ; retVal . put ( key , valsIter . next ( ) ) ; } Preconditions . checkArgument ( ! valsIter . hasNext ( ) , "number of values[%s] exceeds number of keys[%s]" , retVal . size ( ) + Iterators . size ( valsIter ) , retVal . size ( ) ) ; return retVal ; }
Create a Map from iterables of keys and values . Will throw an exception if there are more keys than values or more values than keys .