Initialize Map<String, Object> instance from Map entries

  • A+
Category:Languages

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?

 


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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: