When would I use java Collections singletonMap ...

Basically, it allows you to do this:

callAPIThatTakesAMap(Collections.singletonMap(key, value));

rather than this:

Map<KeyType, ValueType> m = new HashMap<KeyType, ValueType>();

m.put(key, value);

callAPIThatTakesAMap(m);

which is much nicer when you only have a single key/value pair. This situation probably does not arise very often, but singleton() and singletonList() can quite frequently be useful.

参考 :

http://stackoverflow.com/questions/7125536/when-would-i-use-java-collections-singletonmap-method

你可能感兴趣的:(When would I use java Collections singletonMap ...)