Comments on: Java 8, Initializing Maps in the Smartest Way https://speedment.com/java-8-initializing-maps-in-the-smartest-way/ Build Fast Java Applications for the Fastest Business Performance Fri, 01 May 2020 05:21:37 +0000 hourly 1 https://wordpress.org/?v=5.1.1 By: Unknown https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-49 Mon, 08 Apr 2019 22:32:00 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-49 This approach, althought interesting from Academia point of view (functinal Java), is in not IMHO practical.
First and foremost it violates KISS principle.
Second, due to varargs compiler will create array of Objects to hold the arguments, which in turn will preclude JIT optimization on array creation.
And third, generics and varargs to not play good together – this may lead to heap polution.
I still do give credits to Author for approach. I just think that we don't have to force ourselves to make Java pure functional at any cost.

]]>
By: Unknown https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-50 Mon, 21 Jan 2019 22:30:35 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-50 …and isn't it a trip! . I think if you replaced 'K' and 'V' with 'Object', my code would work. Running generics Java code through the browser does just what the Java compiler does after it makes sure all the types match up 🙂

]]>
By: Unknown https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-51 Mon, 21 Jan 2019 22:18:34 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-51 Hey. Sorry everyone. I only realized after posting that all of the generics notations have been removed from my sample code. That same ol' HTML vs generics clash we all know and love. If you keep this in mind, I think the code still makes my point. Just know that I have a working version of this code, property type-safed with generics notations.

]]>
By: Unknown https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-52 Mon, 21 Jan 2019 22:13:08 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-52 I really like your use of the Builder pattern. That's the one that gets my vote.

I'm surprised that neither you nor anyone else has mentioned the fact that the Java 9 "of" functionality won't apply to your example problem. It only goes up to a max of 10 elements, as it simply adds 11 (includes the zero element case) overloads of a static "of" method. It provides no benefit at all really, other than hiding the declaration of 11 ugly methods within the standard library vs one's own code.

As proof, it should be noted that you could add static "of" methods to your builder to get the same Java 9 functionality and also get to your case of 13 elements. For your example, and to be complete, you'd need to add 13 (or 14 with the zero elements case) overloads of "of", which is ugly, but that's all Java 9 does (and again, only to 10 elements). Here's the "of" method for your example:

public static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10, K k11, V v11, K k12, V v12, K k13, V v13) {
MapBuilder builder = builder();
builder.put(k1, v1);
builder.put(k2, v2);
builder.put(k3, v3);
builder.put(k4, v4);
builder.put(k5, v5);
builder.put(k6, v6);
builder.put(k7, v7);
builder.put(k8, v8);
builder.put(k9, v9);
builder.put(k10, v10);
builder.put(k11, v11);
builder.put(k12, v12);
builder.put(k13, v13);
return builder.build();
}

private static Map builderPatternWithOf() {
return Maps.of(0, "zero", 1, "one", 2, "two", 3, "three", 4, "four", 5, "five", 6, "six", 7, "seven", 8, "eight", 9, "nine", 10, "ten", 11, "eleven", 12, "twelve");
}

PS: I've moved on to Kotlin. This is a problem I won't ever encounter unless I'm retrofitting old code I haven't yet converted to Kotlin. I highly recommend Kotlin to all. I like Groovy too, but I like Kotlin better.

]]>
By: Reduce Epilator Pain https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-54 Thu, 27 Dec 2018 05:13:21 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-54 It is nice post.

]]>
By: Instagram Package from likeswatch https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-55 Thu, 27 Dec 2018 05:01:41 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-55 Nice post. I was checking continuously this weblog and I am impressed! Extremely helpful information

]]>
By: Anonymous https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-61 Wed, 03 Oct 2018 14:07:00 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-61 up to 5 entries…

]]>
By: Unknown https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-73 Thu, 25 Jan 2018 10:57:04 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-73 FInally showed up in Java 9 https://docs.oracle.com/javase/9/docs/api/java/util/Map.html#of–

]]>
By: Anonymous https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-74 Tue, 26 Dec 2017 00:36:51 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-74 This a realy great way to do it, simple, clean and efficient

]]>
By: Unknown https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-75 Fri, 17 Nov 2017 16:16:59 +0000 https://speedment.com/java-8-initializing-maps-in-the-smartest-way/#comment-75 This is Java. A couple of pages-long blog post on how to create a map with a couple of elements. And the result is that it still needs a lot of typing to do such a simple thing. Please don't get me wrong: I've been using Java for over 10 years, and will still be using Java, but things like Map.of() (or HashMap.of() ) should already be there since Java 8, when default interface methods have been introduced.

]]>