On 07/09/2015 09:05 PM, Pavel Rappo wrote:
On 9 Jul 2015, at 18:46, Louis Wasserman <[email protected]> wrote:what you can do there is new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5"));Louis, sure we can do this. No problem with that. But what we are really talking about here (as far as I understand) is a convenience. In my opinion there's no much difference between new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5")); and ArrayList<String> l = new ArrayList<>(); l.addAll(Arrays.asList("1", "2", "3", "4", "5")); or ArrayList<String> l = new ArrayList<>(); Arrays.asList("1", "2", "3", "4", "5").forEach(l::add); etc. Thanks for collection types connectivity.
just to be complete , there is also ArrayList<String> l = new ArrayList<>(); Collections.addAll(l, "1", "2", "3", "4", "5"); which avoid the allocation of the intermediary list.
In other words I think the difference between readable and not-so-easily-readable code in this particular example is very subjective. What we have here is a very special case of creating a _small_ list of easily enumerable elements. I hope this work [1] will solve the problem or at least will make it a lot less severe. ------------------------------------------------------------------------------ [1] https://bugs.openjdk.java.net/browse/JDK-8048330
cheers, Rémi
