Converts array into a java.util.Map. : Array Collections « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » Array CollectionsScreenshots 
Converts array into a java.util.Map.
    
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:[email protected]">Fredrik Westermarck</a>
 @author Nikolay Metchev
 @author Matthew Hawthorne
 @author Tim O'Brien
 @author Pete Gieser
 @author Gary Gregory
 @author <a href="mailto:[email protected]">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
  //-----------------------------------------------------------------------
  /**
   * <p>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.</p>
   *
   * <p>This method can be used to initialize:</p>
   * <pre>
   * // Create a Map mapping colors.
   * Map colorMap = MapUtils.toMap(new String[][] {{
   *     {"RED", "#FF0000"},
   *     {"GREEN", "#00FF00"},
   *     {"BLUE", "#0000FF"}});
   * </pre>
   
   * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
   *
   @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.Entryobject;
              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;
  }
}

   
    
    
    
  
Related examples in the same category
1.Array Iterator
2.Array MapArray Map
3.Array SetArray Set
4.Array Int Set
5.Remove duplicate element from array
6.Convert an Array to a List
7.Converting an Array to a Collection
8.Converting a Collection of user objects to an Array
9.Create an array containing the elements in a set
10.Convert an array to a Map
11.Converting a Collection of String to an ArrayConverting a Collection of String to an Array
12.Treating an Array as an Enumeration
13.ArrayEnumeration class (implements Enumeration)ArrayEnumeration class (implements Enumeration)
14.Custom ArrayMap implementation (extends AbstractMap)Custom ArrayMap implementation (extends AbstractMap)
15.Custom ArraySet implementation (extends AbstractSet)Custom ArraySet implementation (extends AbstractSet)
16.Growable array of intsGrowable array of ints
17.Growable array of floats.
18.Acts like an java.util.ArrayList but for primitive int valuesActs like an java.util.ArrayList but for primitive int values
19.Acts like an java.util.ArrayList but for primitive long values
20.Add array to collection
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.