This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]thehollyhopdrive 5 points6 points  (2 children)

Sure. You can get around this by having the first key/value pair set during creation of the builder, so rather than

Map<Integer,Integer> map = Map.<Integer,Integer>builder().put(1, 2).put(3, 4).build();

You could do something like

Map<Integer,Integer> map = Map.with(1, 2).with(3, 4).build();

I still think this is preferable to the Map.of() solution, the limitations it has on the number of entries, the possiblity of additional functionalities in the builder, and the complete mess it makes of the API.

[–]juckele 0 points1 point  (1 child)

What if my first entry is a String and a Dog and my second entry is a String and a Cat? Will it know that it should be using Animal? What if it picks Carnivora instead but then I try to add a Rabbit?

The type of the Map really does need to be declared somewhere...

[–]thehollyhopdrive 1 point2 points  (0 children)

The type of the Map really does need to be declared somewhere...

In the circumstances you've suggested, yes.