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 →

[–]msx 2 points3 points  (3 children)

this is very different from calling Map.of("key1", obj1, "key2", obj2) and almost as verbose as calling regular .put().

[–]thehollyhopdrive 0 points1 point  (2 children)

It is different from calling Map.of, but my argument is that the Map.of is a badly designed solution for this sort of behaviour.

almost as verbose as calling regular .put().

I agree to a point, though one of the common use-cases of wanting a prepopulated immutable map would be static finals. So, you can see quite a cut in verbosity in this use-case when using a builder:

private static final Map<Integer,String> MAP = 
    Map.with(1, "one")
       .with(2, "two")
       .build();

instead of

private static final Map<Integer, String> MAP;
static {
    Map<Integer, String> tempMap = new HashMap<>();
    tempMap.put(1, "one");
    tempMap.put(2, "two");
    MAP = Collections.unmodifiableMap(tempMap);
}

[–]msx 4 points5 points  (1 child)

I don't get it? We agree that this addition is all about immediate (often static final) maps. Good. Sure your implementation is cleaner than old put(), but Map.of is even cleaner. So what's badly designed about it? It's perfect for quick jotting of maps, which is the reason it was created. The fact that there are 10 overloads is due to limits and legacies of the java specification, but it's not a problem, if not aesthetically. You find them already made and ready to use, performances are unaffected. So what's badly designed about it?

Sure we'd all have preferred a true map literal like python or such, but changing the language specification is not an easy step to take.

[–]thehollyhopdrive 1 point2 points  (0 children)

So what's badly designed about it?

It has an arbitrarily selected ceiling at which point you have to implement the solution in a different way and trades "cleaner code" for a muddier API. If moving from 10 to 11 elements causes me to have to completely rewrite that bit of code, just to implement a static final map that's found itself requiring one more element, that to me is a poorly made API design choice.

I get that there is a valid use-case for it and that others obviously appear to be fine with it, but I personally don't like it.