13

Say I have some map entries like so:

var a = Map.entry("a", new Object());
var b = Map.entry("b", new Object());
var c = Map.entry("c", new Object());

var m = Map.of(a,b,c);  // error here

I get this error:

Cannot resolve method 'of(java.util.Map.Entry, java.util.Map.Entry, java.util.Map.Entry)'

I just want to make a new Map from entries in a map, how can I do this? Not the question is specifically about how to init a Map given Map.Entry instances.

  • 1
    I think Map.ofEntries will suffice. Suprising that Map.of is not overloaded to support entries, but I guess I know why. –  Feb 04 '19 at 05:34
  • 1
    @RobbyCornelissen it is Java version 11, the var keyword was introduced, please –  Feb 04 '19 at 05:34
  • Actually it goes back to Java version 10, not 11, so.. –  Feb 04 '19 at 05:41
  • 5
    more precisely `java-9` – Vishwa Ratna Feb 04 '19 at 05:48
  • @rakim from a [linked answer](https://stackoverflow.com/a/46601978/1746118) ... *The reason for that is since there is a varargs implementation for `List.of` and `Set.of` but to **create a similar API for Map both the keys and values were supposed to be boxed** as stated in the JEP as well. So, the same was created using varargs of type Map.entry()*... aside: have updated the tag to java-9 instead – Naman Feb 04 '19 at 05:59
  • 2
    @CommonMan The `var` keyword was introduced in Java 10 not Java 9. – Mark Rotteveel Feb 04 '19 at 10:07
  • 1
    You're lucky you passed an odd number of arguments to `Map.of`. If you had passed an even number of arguments, `m` would have been a map of entry to entry, specifically, `Map,Map.Entry>`. – Stuart Marks Feb 13 '19 at 20:05

5 Answers5

17

Replace

Map.of(a,b,c); 

with

Map.ofEntries(a,b,c);

If you want to still use Map.of() then you shall paste keys and values explicitly.

Map.Entry() returns an immutable Map.Entry containing the given key and value. These entries are suitable for populating Map instances using the Map.ofEntries() method.

When to use Map.of() and when to use Map.ofEntries()

Naman
  • 27,789
  • 26
  • 218
  • 353
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
4

From jdk-9 you can use Map.of() to create Map with key value pairs

Map<String, Object> map = Map.of("a", new Object(), "b", new Object(), "c", new Object());

And also by using SimpleEntry

Map<String, Object> map = Map.ofEntries(
  new AbstractMap.SimpleEntry<>("a", new Object()),
  new AbstractMap.SimpleEntry<>("b", new Object()),
  new AbstractMap.SimpleEntry<>("c", new Object()));

Or by using Map.ofEntries OP suggestion

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
3

The simple answer is:

var a = Map.entry("a", new Object());
var b = Map.entry("b", new Object());
var c = Map.entry("c", new Object());

var m = Map.ofEntries(a,b,c);  // ! use Map.ofEntries not Map.of

And the type of Map.entry(key,val) is Map.Entry<K,V>, in case you were wondering.

2

Use this

var m = Map.ofEntries(a, b, c);

instead of

var m = Map.of(a,b,c);
Mukit09
  • 2,956
  • 3
  • 24
  • 44
2

To create a map from entries Use either:

var a = Map.entry("a", new Object());
var b = Map.entry("b", new Object());
var c = Map.entry("c", new Object());

var m = Map.ofEntries(a,b,c);

or:

var m = Map.ofEntries(
             entry("a", new Object()),
             entry("b", new Object()),
             entry("c", new Object()));

You can also create the map without explicitly creating the entries:

var m = Map.of("a", new Object(),
               "b", new Object(),
               "c", new Object());
ETO
  • 6,970
  • 1
  • 20
  • 37