<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><p>Dear Amber developers,</p><p>I recently watched the JavaONE 2025 session titled <em>“A New Model for Java Object Initialization”</em> and was particularly intrigued by the proposed improvements to array initialization.</p><p><a href="https://www.youtube.com/watch?v=XtvR4kqK8lo" target="_blank">https://www.youtube.com/watch?v=XtvR4kqK8lo</a></p><p><br></p><p>I strongly agree that Java needs better mechanisms for initializing data structures in a concise, expressive, and stricter manner—similar in spirit to Python’s list comprehensions and aligned with Strict initialization, required for Valhalla. Such constructs can help avoid subtle bugs and the presence of unintended <code>null</code> values. However, I remain skeptical about the decision to focus this new model exclusively around arrays.</p><p>As has been discussed over the past few months, arrays are not ideal as a default abstraction, especially for students or in enterprise applications. Arrays are a low-level construct with several limitations:</p><ul><li><p>They do not integrate well with generics.</p></li><li><p>They are of fixed size.</p></li><li><p>They lack methods and flexibility.</p></li><li><p>They are syntactically and semantically inconsistent with the rest of the Java Collections Framework.</p></li></ul><p>In many ways, arrays are a legacy feature inherited from C/C++—much like the original <code>switch</code> statement—that carry forward certain limitations that Java has otherwise worked hard to overcome.</p><p>Given these issues, Why not just create an small API that facilitates the creation of the most used data structures with strict initialization? </p><p>For example:</p><p><br></p><p>void main(){</p><p><br></p><p>    // toArray</p><p>    var array = StrictCollections.toArray(String.class, 5, i -> "Item-" + i);</p><p>    IO.println("Array: " + Arrays.toString(array));</p><p><br></p><p>    // toList</p><p>    var list = StrictCollections.toList(5, i -> "List-" + i);</p><p>    IO.println("List: " + list);</p><p><br></p><p>    // toSet</p><p>    var set = StrictCollections.toSet(5, i -> "Set-" + (i % 3));</p><p>    IO.println("Set: " + set);</p><p><br></p><p><br></p><p>    var map = StrictCollections.toMap(</p><p>        5,</p><p>        i -> "Key-" + i,</p><p>        i -> i * 100</p><p>    );</p><p>    IO.println("Map: " + map);</p><p><br></p><p>   </p><p>}</p><p><br></p><p><br></p><p>public static class StrictCollections {</p><p><br></p><p>    public static <T> T[] toArray(Class<T> clazz, int size, IntFunction<T> function) {</p><p>        @SuppressWarnings("unchecked")</p><p>        T[] array = (T[]) Array.newInstance(clazz, size); // This could be a frozen array once these are ready</p><p>        for (int i = 0; i < size; i++) {</p><p>            array[i] = function.apply(i);</p><p>        }</p><p>        return array;</p><p>    }</p><p><br></p><p>    public static <T> ArrayList<T> toList(int size, IntFunction<T> function) {</p><p>        var list = new ArrayList<T>(size);</p><p>        for (int i = 0; i < size; i++) {</p><p>            list.add(function.apply(i));</p><p>        }</p><p>        return list;</p><p>    }</p><p><br></p><p>    public static <T> HashSet<T> toSet(int size, IntFunction<T> function) {</p><p>        List<T> list = new ArrayList<>(size);</p><p>        for (int i = 0; i < size; i++) {</p><p>            list.add(function.apply(i));</p><p>        }</p><p>        return new HashSet<>(list);</p><p>    }</p><p><br></p><p>    public static <K, V> HashMap<K, V> toMap(int size, IntFunction<K> kFunction, IntFunction<V> vFunction) {</p><p>        HashMap<K, V> map = new HashMap<>(size);</p><p>        for (int i = 0; i < size; i++) {</p><p>            map.put(kFunction.apply(i), vFunction.apply(i));</p><p>        }</p><p>        return map;</p><p>    }</p><p>}</p><p>While this is admittedly a rough sketch developed in just a few minutes, I believe a similar—much more thoroughly designed—approach could provide much greater flexibility with far less complexity than introducing a dedicated array-specific feature. It would also extend naturally to a broader range of use cases --Such as being able to be combined with the Stream API in a much more ergonomic way--. Furthermore, as value classes and parametric JVM start to make it into the language and the JVM, the advantages of arrays and primitive types will diminish further. In that context, arrays will become even less compelling in the future.</p><p>If Java is to introduce a safe, expressive, and idiomatic strict initialization literal for data structures, I would argue it should primarily support <code>List</code>, <code>Set</code>, and <code>Map</code>—especially <code>Map</code>, which remains one of the least ergonomic structures to initialize in the language today, particularly when compared to alternatives in Dart, Python, or even JavaScript objects. Data structures that are much more used.</p><p><br></p><p>Thank you so much for all your work and always yours</p><p><br></p></div></div></div>
</div></div>