All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alee.utils.MapUtils Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see .
 */

package com.alee.utils;

import com.alee.utils.collection.DoubleMap;

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

/**
 * This class provides a set of utilities to work with various maps.
 *
 * @author Mikle Garin
 */

public final class MapUtils
{
    /**
     * Returns copied Map.
     *
     * @param map Map to copy
     * @param  Map key type
     * @param  Map value type
     * @return copied Map
     */
    public static  Map copyMap ( final Map map )
    {
        return new HashMap ( map );
    }

    /**
     * Returns copied HashMap.
     *
     * @param map HashMap to copy
     * @param  HashMap key type
     * @param  HashMap value type
     * @return copied HashMap
     */
    public static  HashMap copyHashMap ( final HashMap map )
    {
        return new HashMap ( map );
    }

    /**
     * Returns copied LinkedHashMap.
     *
     * @param map LinkedHashMap to copy
     * @param  LinkedHashMap key type
     * @param  LinkedHashMap value type
     * @return copied LinkedHashMap
     */
    public static  LinkedHashMap copyLinkedHashMap ( final LinkedHashMap map )
    {
        return new LinkedHashMap ( map );
    }

    /**
     * Returns copied DoubleMap.
     *
     * @param map DoubleMap to copy
     * @param  DoubleMap key type
     * @param  DoubleMap value type
     * @return copied DoubleMap
     */
    public static  DoubleMap copyDoubleMap ( final DoubleMap map )
    {
        return new DoubleMap ( map );
    }

    /**
     * Returns Map with cloned values.
     *
     * @param map Map to clone
     * @param  Map key type
     * @param  Map value type
     * @return cloned Map
     */
    public static  Map cloneMap ( final Map map )
    {
        final Map clone = new HashMap ( map.size () );
        for ( final Map.Entry entry : map.entrySet () )
        {
            clone.put ( entry.getKey (), ReflectUtils.cloneSafely ( entry.getValue () ) );
        }
        return clone;
    }

    /**
     * Returns HashMap with cloned values.
     *
     * @param map HashMap to clone
     * @param  HashMap key type
     * @param  HashMap value type
     * @return cloned HashMap
     */
    public static  HashMap cloneHashMap ( final HashMap map )
    {
        final HashMap clone = new HashMap ( map.size () );
        for ( final Map.Entry entry : map.entrySet () )
        {
            clone.put ( entry.getKey (), ReflectUtils.cloneSafely ( entry.getValue () ) );
        }
        return clone;
    }

    /**
     * Returns LinkedHashMap with cloned values.
     *
     * @param map LinkedHashMap to clone
     * @param  LinkedHashMap key type
     * @param  LinkedHashMap value type
     * @return cloned LinkedHashMap
     */
    public static  LinkedHashMap cloneLinkedHashMap ( final LinkedHashMap map )
    {
        final LinkedHashMap clone = new LinkedHashMap ( map.size () );
        for ( final Map.Entry entry : map.entrySet () )
        {
            clone.put ( entry.getKey (), ReflectUtils.cloneSafely ( entry.getValue () ) );
        }
        return clone;
    }

    /**
     * Returns DoubleMap with cloned values.
     *
     * @param map DoubleMap to clone
     * @param  DoubleMap key type
     * @param  DoubleMap value type
     * @return cloned DoubleMap
     */
    public static  DoubleMap cloneLinkedHashMap ( final DoubleMap map )
    {
        final DoubleMap clone = new DoubleMap ( map.size () );
        for ( final Map.Entry entry : map.entrySet () )
        {
            clone.put ( entry.getKey (), ReflectUtils.cloneSafely ( entry.getValue () ) );
        }
        return clone;
    }

    /**
     * Returns newly created HashMap with the specified key and value pair added.
     *
     * @param key   key to add
     * @param value value to add
     * @param    key type
     * @param    value type
     * @return newly created HashMap
     */
    public static  HashMap newHashMap ( final K key, final V value )
    {
        final HashMap map = new HashMap ( 1 );
        map.put ( key, value );
        return map;
    }

    /**
     * Returns newly created LinkedHashMap with the specified key and value pair added.
     *
     * @param key   key to add
     * @param value value to add
     * @param    key type
     * @param    value type
     * @return newly created LinkedHashMap
     */
    public static  HashMap newLinkedHashMap ( final K key, final V value )
    {
        final LinkedHashMap map = new LinkedHashMap ( 1 );
        map.put ( key, value );
        return map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy