add key-value pair to map, and key need not null or empty - Android java.util

Android examples for java.util:Map

Description

add key-value pair to map, and key need not null or empty

Demo Code


//package com.java2s;
import android.text.TextUtils;

import java.util.Map;

public class Main {
    /**//from w  w w .  jav a  2s . c  om
     * add key-value pair to map, and key need not null or empty
     * 
     * @param map
     * @param key
     * @param value
     * @return <ul>
     *         <li>if map is null, return false</li>
     *         <li>if key is null or empty, return false</li>
     *         <li>return {@link Map#put(Object, Object)}</li>
     *         </ul>
     */
    public static boolean putMapNotEmptyKey(Map<String, String> map,
            String key, String value) {
        if (map == null || TextUtils.isEmpty(key)) {
            return false;
        }

        map.put(key, value);
        return true;
    }

    /**
     * is null or its size is 0
     * 
     * <pre>
     * isEmpty(null)   =   true;
     * isEmpty({})     =   true;
     * isEmpty({1, 2})    =   false;
     * </pre>
     * 
     * @param sourceMap
     * @return if map is null or its size is 0, return true, else return false.
     */
    public static <K, V> boolean isEmpty(Map<K, V> sourceMap) {
        return (sourceMap == null || sourceMap.size() == 0);
    }
}

Related Tutorials