Java Map to Properties mapToProperties(Map map)

Here you can find the source of mapToProperties(Map map)

Description

Converts a map into an array of strings.

License

Open Source License

Parameter

Parameter Description
map the map to convert.

Return

an array representation of the map with keys occupying even indices in the array and corresponding values occupying the successive odd indices.

Declaration

public static String[] mapToProperties(Map<String, String> map) 

Method Source Code

//package com.java2s;
/*//  w  ww .ja v a 2  s.c  om
 * TextUtilities.java (Class: com.madphysicist.tools.util.TextUtilities)
 *
 * Mad Physicist JTools Project (General Purpose Utilities)
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2012 by Joseph Fox-Rabinovitz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

import java.util.Map;

public class Main {
    /**
     * Converts a map into an array of strings. The array will be a sequence of
     * pair of strings, each consisting of a map key followed by its value. The
     * result is similar to a MATLAB property list. See the
     * <a href="#Properties">class description</a> for more about property lists.
     * The array will be null if the map is null, and empty if the input is
     * empty. The returned property list is not guaranteed to be sorted in any
     * particular order.
     *
     * @param map the map to convert.
     * @return an array representation of the map with keys occupying even
     * indices in the array and corresponding values occupying the successive
     * odd indices.
     * @since 1.0.0
     */
    public static String[] mapToProperties(Map<String, String> map) {
        if (map == null)
            return null;
        String[] properties = new String[map.size() * 2];
        int counter = 0;
        for (Map.Entry<String, String> entry : map.entrySet()) {
            properties[counter++] = entry.getKey();
            properties[counter++] = entry.getValue();
        }
        return properties;
    }
}

Related

  1. mapToProperties(Map params)
  2. mapToProperties(Map propertyMap)