safe Get from Map - Android java.util

Android examples for java.util:Map

Description

safe Get from Map

Demo Code


//package com.book2s;
import java.util.Map;

public class Main {
    public static <K, V> V safeGet(Map<K, V> map, K key, V defaultValue) {
        if (map == null) {
            return null;
        } else {/*from   w  w w . j  a  v a2 s  .c o m*/
            return map.containsKey(key) ? map.get(key) : defaultValue;
        }
    }

    public static <K, V> V safeGet(Map<K, V> map, K key) {
        if (map == null) {
            return null;
        } else {
            return map.containsKey(key) ? map.get(key) : null;
        }
    }
}

Related Tutorials