添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
You can check for key existence and return default. Or extend the class and modify the behavior. or even you can use null - and put some check wherever you want to use it. SudhirJ Sep 22 '11 at 18:10 This is related / duplicate of stackoverflow.com/questions/4833336/… some other options are discussed there. Mark Butler Dec 24 '12 at 8:58

[Update]

As noted by other answers and commenters, as of Java 8 you can simply call Map#getOrDefault(...) .

[Original]

There's no Map implementation that does this exactly but it would be trivial to implement your own by extending HashMap:

public class DefaultHashMap<K,V> extends HashMap<K,V> {
  protected V defaultValue;
  public DefaultHashMap(V defaultValue) {
    this.defaultValue = defaultValue;
  @Override
  public V get(Object k) {
    return containsKey(k) ? super.get(k) : defaultValue;
                Just to be precise, you may want to adjust the condition from (v == null) to (v == null && !this.containsKey(k)) in case they purposely added a null value. I know, this is just a corner case, but the author may run into it.
                    – Adam Paynter
                Sep 22 '11 at 18:17
                @maerics: I noticed that you used !this.containsValue(null). This is subtly different from !this.containsKey(k). The containsValue solution will fail if some other key has been explicitly assigned a value of null. For example: map = new HashMap(); map.put(k1, null); V v = map.get(k2); In this case, v will still be null, correct?
                    – Adam Paynter
                Sep 22 '11 at 18:42
                In general, I think this is a bad idea - I'd push the defaulting behavior into the client, or a delegate that doesn't claim to be a Map.  In particular, the lack of valid keySet() or entrySet() will cause problems with anything that expects the Map contract to be respected.  And the infinite set of valid keys that containsKey() implies is likely to cause bad performance that's hard to diagnose.  Not to say, though, that it might not serve some particular purpose.
                    – Ed Staub
                Sep 22 '11 at 18:48
                One problem with this approach is if the value is a complicated object.  Map<String, List>#put won't work as expected.
                    – Eyal
                Nov 5 '14 at 14:53
                Does not work on ConcurrentHashMap. There, you should check get's result for null.
                    – dveim
                Nov 6 '15 at 17:15
                getOrDefault is very nice, but requires the default definition every time the map is accessed.  Defining a default value once would also have readability benefits when creating a static map of values.
                    – ach
                Aug 4 '14 at 14:19
                This is trivial to implement yourself. private static String get(Map map, String s) { return map.getOrDefault(s, "Error"); }
                    – Jack Satriano
                Mar 10 '15 at 18:08
                @JackSatriano Yeah but you'd have to hard-code the default value, or have a static variable for it.
                    – Blrp
                Jan 14 '16 at 13:20
                See below the answer using computeIfAbsent, better when the default value is expensive or should be different each time.
                    – hectorpal
                Jun 22 '17 at 5:43
                Though it is worse for memory, and will only save computation time if the default value is expensive to construct / compute. If it's cheap, you'll probably find it performs worse, as it has to insert into the map, rather than just returning a default value. Certainly another option though.
                    – Spycho
                Jun 22 '17 at 12:23
        

Use Commons' DefaultedMap if you don't feel like reinventing the wheel, e.g.,

Map<String, String> map = new DefaultedMap<>("[NO ENTRY FOUND]");
String surname = map.get("Surname"); 
// surname == "[NO ENTRY FOUND]"

You can also pass in an existing map if you're not in charge of creating the map in the first place.

+1 although sometimes its easier to reinvent the wheel than to introduce large dependencies for tiny slices of simple functionality. – maerics Sep 22 '11 at 19:39 and funny thing is, that many projects that I work with already have something like this in classpath (either Apache Commons or Google Guava) – bartosz.r Feb 20 '13 at 15:53

Java 8 introduced a nice computeIfAbsent default method to Map interface which stores lazy-computed value and so doesn't break map contract:

Map<Key, Graph> map = new HashMap<>();
map.computeIfAbsent(aKey, key -> createExpensiveGraph(key));

Origin: http://blog.javabien.net/2014/02/20/loadingcache-in-java-8-without-guava/

Disclamer: This answer doesn't match exactly what OP asked but may be handy in some cases matching question's title when keys number is limited and caching of different values would be profitable. It shouldn't be used in opposite case with plenty of keys and same default value as this would needlessly waste memory.

Not what OP asked: he wants no side effect on the map. Also, storing the default value for each absent key is a useless loss of memory space. – numéro6 Jul 12 '17 at 13:19 @numéro6, yes, this doesn't match exactly what OP asked but some googling people still find this side answer useful. As other answers mentioned, it's impossible to achieve exactly what OP asked without breaking map contract. Another workaround not mentioned here is to use another abstraction instead of Map. – Vadzim Jul 12 '17 at 13:56 It's possible to achieve exactly what OP asked without breaking map contract. No workaround is needed, just using getOrDefault is the right (most updated) way, computeIfAbsent is the wrong way: you will lose time calling the mappingFunction and memory by storing the result (both for each missing keys). I can't see any good reason to do that instead of getOrDefault. What I'm describing is the exact reason why there are two distinct methods on the Map contract: there are two distinct use-cases that should not be confused (I had to fix some at work). This answer spread the confusion. – numéro6 Jul 13 '17 at 15:01

Can't you just create a static method that does exactly this?

private static <K, V> V getOrDefault(Map<K,V> map, K key, V defaultValue) {
    return map.containsKey(key) ? map.get(key) : defaultValue;
        

You can simply create a new class that inherits HashMap and add getDefault method. Here is a sample code:

public class DefaultHashMap<K,V> extends HashMap<K,V> {
    public V getDefault(K key, V defaultValue) {
        if (containsKey(key)) {
            return get(key);
        return defaultValue;

I think that you should not override get(K key) method in your implementation, because of the reasons specified by Ed Staub in his comment and because you will break the contract of Map interface (this can potentially lead to some hard-to-find bugs).

You've got a point in not overriding the get method. On the other hand - your solution doesn't allow using the class via interface, which might often be the case. – Krzysztof Jabłoński Jan 21 '14 at 7:59 @Larry, Seems a little silly to me to subclass HashMap just for this functionality when null is perfectly fine. – mrkhrts Sep 22 '11 at 18:11 It's not fine if you're using a NullObject pattern, though, or don't want to scatter null-checks throughout your code--a desire I completely understand. – Dave Newton Aug 24 '12 at 13:34

When the get(Object) method is called with a key that does not exist in the map, the factory is used to create the object. The created object will be added to the map using the requested key.

This allows you to do something like this:

    Map<String, AtomicInteger> map = LazyMap.lazyMap(new HashMap<>(), ()->new AtomicInteger(0));
    map.get(notExistingKey).incrementAndGet();

The call to get creates a default value for the given key. You specify how to create the default value with the factory argument to LazyMap.lazyMap(map, factory). In the example above, the map is initialized to a new AtomicInteger with value 0.

This has an advantage over the accepted answer in that the default value is created by a factory. What if my default value is List<String> -- using the accepted answer I'd risk using the same list for each new key, rather than (say) a new ArrayList<String>() from a factory. – mrblewog Jan 4 '19 at 8:28 * Extension of TreeMap to provide default value getter/creator. * NOTE: This class performs no null key or value checking. * @author N David Brown * @param <K> Key type * @param <V> Value type public abstract class Hash<K, V> extends TreeMap<K, V> { private static final long serialVersionUID = 1905150272531272505L; * Same as {@link #get(Object)} but first stores result of * {@link #create(Object)} under given key if key doesn't exist. * @param k * @return public V getOrCreate(final K k) { V v = get(k); if (v == null) { v = create(k); put(k, v); return v; * Same as {@link #get(Object)} but returns specified default value * if key doesn't exist. Note that default value isn't automatically * stored under the given key. * @param k * @param _default * @return public V getDefault(final K k, final V _default) { V v = get(k); return v == null ? _default : v; * Creates a default value for the specified key. * @param k * @return abstract protected V create(final K k);

Example Usage:

protected class HashList extends Hash<String, ArrayList<String>> {
    private static final long serialVersionUID = 6658900478219817746L;
    @Override
        public ArrayList<Short> create(Short key) {
            return new ArrayList<Short>();
final HashList haystack = new HashList();
final String needle = "hide and";
haystack.getOrCreate(needle).add("seek")
System.out.println(haystack.get(needle).get(0));
        

I needed to read the results returned from a server in JSON where I couldn't guarantee the fields would be present. I'm using class org.json.simple.JSONObject which is derived from HashMap. Here are some helper functions I employed:

public static String getString( final JSONObject response, 
                                final String key ) 
{ return getString( response, key, "" ); }  
public static String getString( final JSONObject response, 
                                final String key, final String defVal ) 
{ return response.containsKey( key ) ? (String)response.get( key ) : defVal; }
public static long getLong( final JSONObject response, 
                            final String key ) 
{ return getLong( response, key, 0 ); } 
public static long getLong( final JSONObject response, 
                            final String key, final long defVal ) 
{ return response.containsKey( key ) ? (long)response.get( key ) : defVal; }
public static float getFloat( final JSONObject response, 
                              final String key ) 
{ return getFloat( response, key, 0.0f ); } 
public static float getFloat( final JSONObject response, 
                              final String key, final float defVal ) 
{ return response.containsKey( key ) ? (float)response.get( key ) : defVal; }
public static List<JSONObject> getList( final JSONObject response, 
                                        final String key ) 
{ return getList( response, key, new ArrayList<JSONObject>() ); }   
public static List<JSONObject> getList( final JSONObject response, 
                                        final String key, final List<JSONObject> defVal ) { 
    try { return response.containsKey( key ) ? (List<JSONObject>) response.get( key ) : defVal; }
    catch( ClassCastException e ) { return defVal; }
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.3.12.36272