There are utility methods to create ImmutableMap
like Immutable.of(Key, value)
and its overload.
But such methods don't exist for HashMap
or LinkedHashMap
in Maps
class.
Is there any better way to do this or Guava assumes such a map is always a constant map and ImmutableMap
is best option to go with and don't need to provide a utility for HashMap
.
-
2Collections.singletonMap – bestsss May 10 '11 at 09:23
-
4he's asking for a mutable map, and to support more than just one entry. – Kevin Bourrillion May 10 '11 at 14:57
-
@kevin, you mean a mutable map w/ just one entry, what's exactly the point of? If you imply a general hashmap, you'd be better off initializing it w/ the expected capacity than just a single key-value (which makes little to no sense to me) – bestsss May 10 '11 at 15:02
5 Answers
Why would you want those for a regular HashMap
or LinkedHashMap
? You can just do this:
Map<String, Object> map = Maps.newHashMap();
map.put(key, value);
The thing with ImmutableMap
is that it is a little bit more cumbersome to create; you first need to make a Builder
, then put the key-value pairs in the builder and then call build()
on it to create your ImmutableMap
. The ImmutableMap.of()
method makes it shorter to write if you want to create an ImmutableMap
with a single key-value pair.
Consider what you'd have to write if you wouldn't use the ImmutableMap.of()
method:
ImmutableMap<String, Object> map = ImmutableMap.builder()
.put(key, value);
.build();
-
@Jasper - If I'm not mistaken support to create a Map like Map
map = {"key" : 1}; will be added in java 8.. if it sounds good then why not have such utilities – Premraj May 10 '11 at 09:38 -
@Falcon Yes, support for collection literals is one of the features that has been proposed for Java 8. This will mainly be useful for "fixed" maps, for example for a map that you initialize once and then never change the content. But for that purpose you should be using `ImmutableMap` if you're using Guava, instead of a regular mutable `HashMap`. – Jesper May 10 '11 at 10:01
-
2Java 8?:) I believe it will be similar to Java 7 - overdue and overpromising:)... – Gabriel Ščerbák May 10 '11 at 17:09
-
@Gabriel Java 7 is coming out this summer (end of July) and collection literals will unfortunately not be in there... Java 8 is planned for the end of 2012. – Jesper May 10 '11 at 19:23
-
I know, if everything was according to plans, we would have Java 7 already here with lambdas... – Gabriel Ščerbák May 10 '11 at 20:48
-
-
2
Try Maps.newHashMap(ImmutableMap.of(...))

- 14,226
- 4
- 44
- 89
-
I see two `newHashMap` methods on that page: one takes no arguments at all and the other takes a `Map`. Which one of those would your code call? – Joachim Sauer May 10 '11 at 09:33
-
5Ok, `Maps.newHashMap(ImmutableMap.of(...))` is short to write, but it's not very efficient: you're first creating an `ImmutableMap` and then you copy its contents to a regular `HashMap`, and then throw the temporary `ImmutableMap` away... – Jesper May 10 '11 at 12:12
-
The difference is that for an immutable map, you have to provide everything up-front, because you can't change it after construction. For mutable maps, you can just create the map and then add the entries. Admittedly this makes it slightly harder to create a map in a single expression, but that doesn't tend to be a problem where you'd want a mutable map anyway, in my experience.

- 1,421,763
- 867
- 9,128
- 9,194
-
You can create a mutable map and expose only the non-mutable interface. (The underlying map is still mutable) – bestsss May 10 '11 at 09:24
-
@bestsss: You can do so, yes... but I'd say it's relatively rare that you want to do that *and* initialize it in a single expression. – Jon Skeet May 10 '11 at 09:26
-
sure, yet I guess it depends what people assume as single expression. A function that takes an Object[][] or Object[], Object[] can do so, and many developers would praise it for being cool. – bestsss May 10 '11 at 09:28
-
If I'm not mistaken support to create a Map like Map
map = {"key" : 1}; will be added in java 8.. if it sounds good then why not have such utilities – Premraj May 10 '11 at 09:37 -
@Falcon, the generics made java harder to parse/read (just imagine the case when you have class name clashes). It's relatively rare (like Jon implies) you need to create a map by a single expression, it's doable now and I see no reason to 'improve' the language. Only configurations come to me as truly usable scenario and I prefer to keep them in xml. – bestsss May 10 '11 at 09:47
ImmutableMap.of()
returns a hash based immutable map without order.
If you need ordered immutable map, ImmutableSortedMap.of()
is a choice.
ImmutableSortedMap provides methods such as firstKey()
, lastKey()
, headMap(K)
and tailMap(K)
;
Both classes provide copyOf(Map)
method.

- 31,208
- 22
- 85
- 130