get value by key from a may Or return Default - Java java.util

Java examples for java.util:Map Value

Description

get value by key from a may Or return Default

Demo Code


//package com.java2s;

import java.util.Map;

public class Main {
    @SuppressWarnings("unchecked")
    public static <K, V> V getOrDefault(Map<K, ?> map, K key, V defaultValue) {
        final Object v = map.get(key);
        if (v == null)
            return defaultValue;
        else/*from  w ww . ja  v  a 2 s .  c om*/
            return (V) v;
    }
}

Related Tutorials