Converts array into a java.util.Map. : Customized Map « Collections « Java Tutorial






import java.util.HashMap;
import java.util.Map;

/* 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *
 */


/**
 * @author Stephen Colebourne
 * @author Moritz Petersen
 * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
 * @author Nikolay Metchev
 * @author Matthew Hawthorne
 * @author Tim O'Brien
 * @author Pete Gieser
 * @author Gary Gregory
 * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
 * @author Maarten Coene
 * @since 2.0
 * @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
 */
public class Main {

  // To map
  //-----------------------------------------------------------------------
  /**
   * Converts the given array into a {@link java.util.Map}. Each element of the array
   * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
   * elements, where the first element is used as key and the second as
   * value.
   *
   * This method can be used to initialize:
   * <pre>
   * // Create a Map mapping colors.
   * Map colorMap = MapUtils.toMap(new String[][] {{
   *     {"RED", "#FF0000"},
   *     {"GREEN", "#00FF00"},
   *     {"BLUE", "#0000FF"}});
   * </pre>
   * 
   * This method returns <code>null</code> for a <code>null</code> input array.
   *
   * @param array  an array whose elements are either a {@link java.util.Map.Entry} or
   *  an Array containing at least two elements, may be <code>null</code>
   * @return a <code>Map</code> that was created from the array
   * @throws IllegalArgumentException  if one element of this Array is
   *  itself an Array containing less then two elements
   * @throws IllegalArgumentException  if the array contains elements other
   *  than {@link java.util.Map.Entry} and an Array
   */
  public static Map toMap(Object[] array) {
      if (array == null) {
          return null;
      }
      final Map map = new HashMap((int) (array.length * 1.5));
      for (int i = 0; i < array.length; i++) {
          Object object = array[i];
          if (object instanceof Map.Entry) {
              Map.Entry entry = (Map.Entry) object;
              map.put(entry.getKey(), entry.getValue());
          } else if (object instanceof Object[]) {
              Object[] entry = (Object[]) object;
              if (entry.length < 2) {
                  throw new IllegalArgumentException("Array element " + i + ", '"
                      + object
                      + "', has a length less than 2");
              }
              map.put(entry[0], entry[1]);
          } else {
              throw new IllegalArgumentException("Array element " + i + ", '"
                      + object
                      + "', is neither of type Map.Entry nor an Array");
          }
      }
      return map;
  }
}








9.33.Customized Map
9.33.1.Implementation of a bit map of any size, together with static methods to manipulate int, byte and byte[] values as bit maps
9.33.2.A fixed size map implementation.
9.33.3.A memory-efficient hash map.
9.33.4.CaseBlindHashMap - a HashMap extension, using Strings as key values.
9.33.5.Clones a map and prefixes the keys in the clone
9.33.6.Converts array into a java.util.Map.
9.33.7.Ordered Map
9.33.8.A hash map that uses primitive ints for the key rather than objects.
9.33.9.HashNMap stores multiple values by a single key value. Values can be retrieved using a direct query or by creating an enumeration over the stored elements.
9.33.10.A Map where keys are compared by object identity, rather than equals()
9.33.11.A java.util.Map implementation using reference values
9.33.12.A simple hashmap from keys to integers
9.33.13.An IdentityMap that uses reference-equality instead of object-equality
9.33.14.Creates a mutable map from two arrays with keys and values
9.33.15.Fixed size hash map using String values as keys mapped to primitive int values.
9.33.16.Hash map for counting references to Object keys.
9.33.17.Hash map using String values as keys mapped to primitive int values.
9.33.18.IntMap provides a simple hashmap from keys to integers
9.33.19.List ordered map
9.33.20.Lookup table that stores a list of strings
9.33.21.Map implementation Optimized for Strings keys
9.33.22.Map with keys iterated in insertion order