155

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372
JorgeO
  • 2,210
  • 5
  • 20
  • 22

8 Answers8

301

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.

Jonik
  • 80,077
  • 70
  • 264
  • 372
22

Collections.emptyMap()

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
basszero
  • 29,624
  • 9
  • 57
  • 79
  • The problem was that this map can only be applied to a Map object not a HashMap – JorgeO Mar 11 '09 at 20:13
  • 10
    You should (generally) avoid declaring objects of their specific types and use the interface (or abstract parent) instead. Try to avoid "HashMap foo;" and use "Map foo;" instead – TofuBeer Mar 11 '09 at 20:16
15

The emptyMap method of the Collections class.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
9

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.

AndreiM
  • 4,558
  • 4
  • 36
  • 50
7

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

Peter Štibraný
  • 32,463
  • 16
  • 90
  • 116
3

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();
    }
  }

}
Vitalii Fedorenko
  • 110,878
  • 29
  • 149
  • 111
1

What about :

Map<String, String> s = Collections.emptyMap();

OscarRyz
  • 196,001
  • 113
  • 385
  • 569
0

You can use:

Collections<String, String>.emptyMap();

Raz
  • 8,918
  • 3
  • 27
  • 40