Alex Rivera | Logout

Java Map implementation that returns a default value instead of null

Asked 2011-01-28T21:44:10.540
38

I have a Map<String, List<String>> in my code, where I'd avoid potential null pointers if the map's #get() method returned an empty list instead of null. Is there anything like this in the java API? Should I just extend HashMap?

Edit
Report

1 Answer

25

@Jon's answer is a good way to do what you are asking directly.

But is strikes me that what you might be trying to implement is a "multimap"; i.e. a mapping from a key to a collection of values. If that is the case, then you should also look at the multimap classes in Guava or Apache commons collections.

Look at:

answered 2011-01-29T00:47:17.127

Your Answer