Java Properties to Map toMap(Properties properties)

Here you can find the source of toMap(Properties properties)

Description

Copies all elements from properties into a Map.

License

Open Source License

Parameter

Parameter Description
properties a parameter

Return

The map containing all elements

Declaration

public static Map<String, String> toMap(Properties properties) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2010 Cloudsmith Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*ww  w  .  j  a  v  a 2  s .c om*/
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/

import java.util.*;

public class Main {
    /**
     * Copies all elements from <code>properties</code> into a Map. The returned map might be unmodifiable
     * @param properties
     * @return The map containing all elements
     */
    public static Map<String, String> toMap(Properties properties) {
        if (properties == null || properties.isEmpty())
            return emptyMap();

        Map<String, String> props = new HashMap<String, String>(properties.size());
        putAll(properties, props);
        return props;
    }

    /**
     * The emptyMap() method was introduced in Java 1.5 so we need this here to be able to
     * down compile to 1.4.
     * @param <K> The type of the map keys
     * @param <V> The type of the map values
     * @return An empty set
     */
    @SuppressWarnings("unchecked")
    public static <K, V> Map<K, V> emptyMap() {
        return Collections.EMPTY_MAP;
    }

    /**
     * Copies all elements from <code>properties</code> into the given <code>result</code>.
     * @param properties
     * @param result
     */
    public static void putAll(Properties properties, Map<String, String> result) {
        for (Enumeration<Object> keys = properties.keys(); keys.hasMoreElements();) {
            String key = (String) keys.nextElement();
            result.put(key, properties.getProperty(key));
        }
    }
}

Related

  1. toMap(final Properties properties)
  2. toMap(Properties properties)
  3. toMap(Properties properties)
  4. toMap(Properties properties)
  5. toMap(Properties properties)
  6. toMap(Properties properties)
  7. toMap(Properties properties)
  8. toMap(Properties properties)
  9. toMap(Properties props)