Best way to create an empty map in Java

192,653

Solution 1

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. 😎

Solution 2

Collections.emptyMap()

Solution 3

The emptyMap method of the Collections class.

Solution 4

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.

Solution 5

Either Collections.emptyMap(), or if type inference doesn't work in your case,
Collections.<String, String>emptyMap()

Share:
192,653
JorgeO
Author by

JorgeO

Developing a new solution for online sales.

Updated on October 18, 2020

Comments

  • JorgeO
    JorgeO over 3 years

    I need to create an empty map.

    if (fileParameters == null)
        fileParameters = (HashMap<String, String>) Collections.EMPTY_MAP;
    

    The problem is that the above code produces this warning: Type safety: Unchecked cast from Map to HashMap

    What is the best way to create this empty map?

    • jjnguy
      jjnguy about 15 years
      What is your declared type for fileParameters?
    • Brett
      Brett about 15 years
      You'll probably get a ClassCastException too.
    • Steve Kuo
      Steve Kuo about 15 years
      fileParameters should be a Map and not a HashMap.
  • TofuBeer
    TofuBeer about 15 years
    You should (generally) avoid declaring objects of their specific types and use the interface (or abstract parent) instead. Try to avoid "HashMap<String, String> foo;" and use "Map<String, String> foo;" instead
  • Sébastien RoccaSerra
    Sébastien RoccaSerra about 15 years
    In most cases, type inference works (ie. map = Collections.emptyMap() works)
  • Jonik
    Jonik about 15 years
    Yeah, true. I edited the answer to be a little more comprehensive.
  • Andrea Asr Franchi
    Andrea Asr Franchi about 15 years
    You won't be able to modify an empty map created that way.