Android examples for java.util:Map
add key-value pair to map, both key and value need not null or empty
//package com.java2s; import android.text.TextUtils; import java.util.Map; public class Main { /**//from w w w . j a v a2s . c om * add key-value pair to map, both key and value 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>if value is null or empty, return false</li> * <li>return {@link Map#put(Object, Object)}</li> * </ul> */ public static boolean putMapNotEmptyKeyAndValue( Map<String, String> map, String key, String value) { if (map == null || TextUtils.isEmpty(key) || TextUtils.isEmpty(value)) { return false; } map.put(key, value); return true; } /** * add key-value pair to map, key need not null or empty * * @param map * @param key * @param value * @param defaultValue * @return <ul> * <li>if map is null, return false</li> * <li>if key is null or empty, return false</li> * <li>if value is null or empty, put defaultValue, return true</li> * <li>if value is neither null nor empty?put value, return true</li> * </ul> */ public static boolean putMapNotEmptyKeyAndValue( Map<String, String> map, String key, String value, String defaultValue) { if (map == null || TextUtils.isEmpty(key)) { return false; } map.put(key, TextUtils.isEmpty(value) ? defaultValue : 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); } }