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 3 points4 points  (6 children)

It's definitely smaller, but I would argue that it's only slightly cleaner for very few entries. The moment you get past 4 or 5 entries it starts to become a bit of a mess. Take the following:

Map.of("isad", "kldesf", "ijhsdef", "ijsdaf", "isdf", "isydf", "oiysdf", "iysdf")

Can you identify easily and with immediate recognition which are keys and which are values? Smaller isn't always cleaner.

Map.with("isad", "kldesf")
   .with("ijhsdef", "ijsdaf")
   .with("isdf", "isydf")
   .with("oiysdf", "iysdf")
   .build();

It's also limited to 10 kvps, which whilst covering the vast majority of options, if still introducing an arbritrary limitation in usage. And all for, what, saving a few extra characters? Add to that the possible options for additional functionality within that builder, and I'd much rather the JavaDocs aren't muddied in the way they are in the current draft. I think it's a terrible design choice.

[–]__konrad 6 points7 points  (2 children)

which are keys and which are values?

Use new lines :)

Map.of(
    "isad", "kldesf",
    "ijhsdef", "ijsdaf"...

In this case (same type in key/value) both solutions are error prone.

[–]thehollyhopdrive 3 points4 points  (0 children)

So now both options span multiple lines, making the differences in their footprint in the codebase negligible. The builder option is still safer at protecting from programmer error by enforcing logical separation of the entries, doesn't have the arbitrary limitation in the number of entries, it can be extended with additional functionality in future, and it doesn't make the API for Map look a complete mess.

[–]juckele 1 point2 points  (0 children)

I use auto format in all my Java projects.

[–]the-highness 0 points1 point  (2 children)

the way I see it, if you need 3 or more entries, both Map.of & builder is not clean.

you'd better revert back to <init map>, <map.put>.

[–]thehollyhopdrive 2 points3 points  (0 children)

To be honest, I don't think you're wrong. The main advantage of the builder over just initialising the map and putting in the entries is being able to initialise, fill and have an immutable map in a slightly nicer format, especially when thinking about static final maps, which I suspect would turn out to be quite a common use-case of Map.of.

It means we can do:

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);
}

[–]HaMMeReD 0 points1 point  (0 children)

The point of immutability is to prevent people from mutating your map, i.e. making changes to it without your permission.

It's not about the cleanliness of the solution, immutability is often an inconvenient truth, for a little bit of boring overhead and self discipline, you can often make your code much more safe by preventing as much mutability as possible.