We can create an empty Map using the emptyMap() method provided by the Java Collections module. This will form an empty Map that is serializable in nature. The method was introduced in Java 1.5 under the Collections Library.
If you need an instance of HashMap, the best way is: fileParameters = new HashMap<String,String>(); Since Map is an interface, you need to pick some class that instantiates it if you want to create an empty instance. HashMap seems as good as any other - so just use that.
HashMap isEmpty() Method in Java HashMap. isEmpty() method of HashMap class is used to check for the emptiness of the map. The method returns True if no key-value pair or mapping is present in the map else False.
The Static Initializer for a Static HashMap We can also initialize the map using the double-brace syntax: Map<String, String> doubleBraceMap = new HashMap<String, String>() {{ put("key1", "value1"); put("key2", "value2"); }};
1) If the Map can be immutable:
Collections.emptyMap()
// or, in some cases:
Collections.<String, String>emptyMap()
You'll have to use the latter sometimes when the compiler cannot automatically figure out what kind of Map is needed (this is called type inference). For example, consider a method declared like this:
public void foobar(Map<String, String> map){ ... }
When passing the empty Map directly to it, you have to be explicit about the type:
foobar(Collections.emptyMap()); // doesn't compile
foobar(Collections.<String, String>emptyMap()); // works fine
2) If you need to be able to modify the Map, then for example:
new HashMap<String, String>()
(as tehblanx pointed out)
Addendum: If your project uses Guava, you have the following alternatives:
1) Immutable map:
ImmutableMap.of()
// or:
ImmutableMap.<String, String>of()
Granted, no big benefits here compared to Collections.emptyMap()
. From the Javadoc:
This map behaves and performs comparably to
Collections.emptyMap()
, and is preferable mainly for consistency and maintainability of your code.
2) Map that you can modify:
Maps.newHashMap()
// or:
Maps.<String, String>newHashMap()
Maps
contains similar factory methods for instantiating other types of maps as well, such as TreeMap
or LinkedHashMap
.
Update (2018): On Java 9 or newer, the shortest code for creating an immutable empty map is:
Map.of()
...using the new convenience factory methods from JEP 269. 😎
Collections.emptyMap()
The emptyMap method of the Collections class.
If you need an instance of HashMap, the best way is:
fileParameters = new HashMap<String,String>();
Since Map is an interface, you need to pick some class that instantiates it if you want to create an empty instance. HashMap seems as good as any other - so just use that.
Either Collections.emptyMap()
, or if type inference doesn't work in your case,Collections.<String, String>emptyMap()
Since in many cases an empty map is used for null-safe design, you can utilize the nullToEmpty
utility method:
class MapUtils {
static <K,V> Map<K,V> nullToEmpty(Map<K,V> map) {
if (map != null) {
return map;
} else {
return Collections.<K,V>emptyMap(); // or guava ImmutableMap.of()
}
}
}
Similarly for sets:
class SetUtils {
static <T> Set<T> nullToEmpty(Set<T> set) {
if (set != null) {
return set;
} else {
return Collections.<T>emptySet();
}
}
}
and lists:
class ListUtils {
static <T> List<T> nullToEmpty(List<T> list) {
if (list != null) {
return list;
} else {
return Collections.<T>emptyList();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With