Java tutorial
//package com.java2s; /* * This file is part of McIDAS-V * * Copyright 2007-2015 * Space Science and Engineering Center (SSEC) * University of Wisconsin - Madison * 1225 W. Dayton Street, Madison, WI 53706, USA * http://www.ssec.wisc.edu/mcidas * * All Rights Reserved * * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and * some McIDAS-V source code is based on IDV and VisAD source code. * * McIDAS-V is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * McIDAS-V is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser Public License for more details. * * You should have received a copy of the GNU Lesser Public License * along with this program. If not, see http://www.gnu.org/licenses. */ import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; public class Main { /** * Takes arrays of {@code keys} and {@code values} and merges them * together to form a {@link Map}. The returned {@code Map} is a * {@link LinkedHashMap} and is truncated in length to the length of the * shorter parameter. * * <p>This is intended for use as {@literal "varargs"} supplied to * {@link #arr(Object...)}. Rather than doing something ugly like: * <pre> * Map<String, String> mappy = new LinkedHashMap<String, String>(); * mappy.put("key0", "val0"); * mappy.put("key1", "val1"); * ... * mappy.put("keyN", "valN"); * </pre> * * Simply do like so: * <pre> * mappy = zipMap( * arr("key0", "key1", ..., "keyN"), * arr("val0", "val1", ..., "valN")); * </pre> * * <p>The latter approach also allows you to make {@code static final} * {@link Map}s much more easily. * * @param keys Array whose elements will be the keys in a {@code Map}. * @param values Array whose elements will the values in a {@code Map}. * * @return A {@code Map} whose entries are of the form * {@code keys[N], values[N]}. * * @see #arr(Object...) * @see #zipMap(java.util.Collection, java.util.Collection) */ public static <K, V> Map<K, V> zipMap(K[] keys, V[] values) { Map<K, V> zipped = new LinkedHashMap<>(keys.length); for (int i = 0; (i < keys.length && i < values.length); i++) { zipped.put(keys[i], values[i]); } return zipped; } /** * A version of {@link #zipMap(Object[], Object[])} that works with * {@link Collection}s. * * @param keys Items that will be the keys in the resulting {@code Map}. * @param values Items that will be the values in the result {@code Map}. * * @return A {@code Map} whose entries are of the form * {@code keys[N], values[N]}. * * @see #zipMap(Object[], Object[]) */ public static <K, V> Map<K, V> zipMap(Collection<? extends K> keys, Collection<? extends V> values) { Map<K, V> zipped = new LinkedHashMap<>(keys.size()); Iterator<? extends K> keyIterator = keys.iterator(); Iterator<? extends V> valueIterator = values.iterator(); while (keyIterator.hasNext() && valueIterator.hasNext()) { zipped.put(keyIterator.next(), valueIterator.next()); } return zipped; } }