Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Why Map.of() method takes up to 10 key value pairs (rather than unlimited) as input parameter?

 
Ranch Foreman
Posts: 126
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Map.of() method takes up to 10 value pairs as input parameter?
 
Marshal
Posts: 25971
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you can create a Map and fill it with up to 10 entries using one line of code, instead of the 11 lines of code it used to take. You might ask "Why 10 and not 20 variables?" Who knows? Maybe they surveyed a collection of random Java code to find out how often people pre-filled a Map with X entries, or maybe they just picked a reasonable-sounding number.
 
Saloon Keeper
Posts: 12442
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or maybe they thought 10 was a nice round number. A bit of a shame really, because if that was the case it would have been nicer if the maximum was 9, on account of us Java programmers being used to 0-based indexing.

I wish they had added special collection initializer syntax to the language instead, like C# did. In C#, you can initialize a Dictionary in this way:
 
Frank Mi
Ranch Foreman
Posts: 126
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't make my question clear. My question is actually why doesn't it take unlimited numbers of key-value pairs like varargs.
 
Marshal
Posts: 70727
288
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does. It takes Map.Entry<K, V>... as a parameter. Here, only with a different name. The syntax of varargs doesn't allow alternating K... and V... in the same method call. Only one varargs parameter per method call.

The Map interface link I posted shows a way to create entries almost as succinctly as that C# syntax.
 
Frank Mi
Ranch Foreman
Posts: 126
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes sense. This format is good enough. Thank you.

 
Campbell Ritchie
Marshal
Posts: 70727
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooooooooooooooooooooooooooooooooh! A cow! Thank you, whoever it was
 
Hug your destiny! And hug this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic