Collections.emptyMap() vs new HashMap()

It is, in my personal experience admittedly, very useful in cases where an API requires a collection of parameters, but you have nothing to provide. For example you may have an API that looks something like this, and does not allow null references:

public ResultSet executeQuery(String query, Map<String, Object> queryParameters);

If you have a query that doesn’t take any parameters, it’s certainly a bit wasteful to create a HashMap, which involves allocating an array, when you could just pass in the ‘Empty Map’ which is effectively a constant, the way it’s implemented in java.util.Collections.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)