Hi, I'm Philipp Bibik and a 16 jear old free time developer. I was born and live in Germany and have only an bad school english, so sorry for any grammar and spelling mistakes. I have an idea to improve the Java ArrayList API. Lists are one of the most imported and used components in the Java API. Although there are relay well designed, there was always something that annoyed me. If you want to create an ArrayList with "String" as diamond operator and add the Strings "1" to "5" to them, you need to do something like this: ArrayList<String> strings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("3"); strings.add("4"); strings.add("5"); I added a few lines of code the the ArrayList class: /** * @param e array whose elements are to be placed into this list */ @SafeVarargs public ArrayList(E... e) { elementData = e; size = elementData.length; } After adding the lines above I was able to accomplish the same result with 77 chars / 5 lines less code: ArrayList<String> strings = new ArrayList<>("1", "2", "3", "4", "5"); In my opinion adding a constructor like this to the "java.util.ArrayList" class and all other (at least some) classes, implementing the "java.util.List" interface would be a big improvement to Java SE9. Thanks for reading, - Philipp Bibik.
Hi Philipp, there is an issue with your proposal, with ArrayList<Integer> list = new ArrayList<>(1); and ArrayList<Integer> list = new ArrayList<>(1, 2); the first line will create an empty array list, the integer will be used to define the capacity of the list, the second line will create a list with two values. There is an open bug to add a bunch of static methods 'of' to do something similar to what you want, https://bugs.openjdk.java.net/browse/JDK-8026106 this is something that a lot of people will like to have and i hope that this can be integrated in 9. cheers, Rémi On 07/09/2015 05:55 PM, Philipp Bibik wrote:
Hi,
I'm Philipp Bibik and a 16 jear old free time developer. I was born and live in Germany and have only an bad school english, so sorry for any grammar and spelling mistakes.
I have an idea to improve the Java ArrayList API. Lists are one of the most imported and used components in the Java API. Although there are relay well designed, there was always something that annoyed me.
If you want to create an ArrayList with "String" as diamond operator and add the Strings "1" to "5" to them, you need to do something like this:
ArrayList<String> strings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("3"); strings.add("4"); strings.add("5");
I added a few lines of code the the ArrayList class:
/** * @param e array whose elements are to be placed into this list */ @SafeVarargs public ArrayList(E... e) { elementData = e; size = elementData.length; }
After adding the lines above I was able to accomplish the same result with 77 chars / 5 lines less code:
ArrayList<String> strings = new ArrayList<>("1", "2", "3", "4", "5");
In my opinion adding a constructor like this to the "java.util.ArrayList" class and all other (at least some) classes, implementing the "java.util.List" interface would be a big improvement to Java SE9.
Thanks for reading,
- Philipp Bibik.
On 7/9/15 9:21 AM, Remi Forax wrote:
There is an open bug to add a bunch of static methods 'of' to do something similar to what you want, https://bugs.openjdk.java.net/browse/JDK-8026106 this is something that a lot of people will like to have and i hope that this can be integrated in 9.
Hi Remi, Thanks for mentioning this. The main JEP for this effort is https://bugs.openjdk.java.net/browse/JDK-8048330 Although this hasn't been updated in some time, I'm intending to return to this in the near future with an eye toward integrating it into JDK 9. The JEP says that we "might" also include static factory methods for the most common concrete collections. The current thinking is now that we will definitely include them. s'marks
Many people have thought about this kind of thing, but it's complicated. Many people regret having java constructors (instead of factory methods). Many people regret how varargs worked out, because generics and arrays don't mix well. Many people think java should have List and Map literals. See how guava tried to do ImmutableList.of, which works well in practice, but is ugly ("don't look behind the curtain!") On Thu, Jul 9, 2015 at 8:55 AM, Philipp Bibik <philipp@bibik.de> wrote:
Hi,
I'm Philipp Bibik and a 16 jear old free time developer. I was born and live in Germany and have only an bad school english, so sorry for any grammar and spelling mistakes.
I have an idea to improve the Java ArrayList API. Lists are one of the most imported and used components in the Java API. Although there are relay well designed, there was always something that annoyed me.
If you want to create an ArrayList with "String" as diamond operator and add the Strings "1" to "5" to them, you need to do something like this:
ArrayList<String> strings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("3"); strings.add("4"); strings.add("5");
I added a few lines of code the the ArrayList class:
/** * @param e array whose elements are to be placed into this list */ @SafeVarargs public ArrayList(E... e) { elementData = e; size = elementData.length; }
After adding the lines above I was able to accomplish the same result with 77 chars / 5 lines less code:
ArrayList<String> strings = new ArrayList<>("1", "2", "3", "4", "5");
In my opinion adding a constructor like this to the "java.util.ArrayList" class and all other (at least some) classes, implementing the "java.util.List" interface would be a big improvement to Java SE9.
Thanks for reading,
- Philipp Bibik.
Philipp, Wouldn't Arrays.asList <http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-> work for you? e.g. List<String> strings = Arrays.asList("1", "2", "3"); or List<String> strings = Arrays.asList("1", "2", "3", "4", "five"); Regards, Kedar On Thu, Jul 9, 2015 at 8:55 AM, Philipp Bibik <philipp@bibik.de> wrote:
Hi,
I'm Philipp Bibik and a 16 jear old free time developer. I was born and live in Germany and have only an bad school english, so sorry for any grammar and spelling mistakes.
I have an idea to improve the Java ArrayList API. Lists are one of the most imported and used components in the Java API. Although there are relay well designed, there was always something that annoyed me.
If you want to create an ArrayList with "String" as diamond operator and add the Strings "1" to "5" to them, you need to do something like this:
ArrayList<String> strings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("3"); strings.add("4"); strings.add("5");
I added a few lines of code the the ArrayList class:
/** * @param e array whose elements are to be placed into this list */ @SafeVarargs public ArrayList(E... e) { elementData = e; size = elementData.length; }
After adding the lines above I was able to accomplish the same result with 77 chars / 5 lines less code:
ArrayList<String> strings = new ArrayList<>("1", "2", "3", "4", "5");
In my opinion adding a constructor like this to the "java.util.ArrayList" class and all other (at least some) classes, implementing the "java.util.List" interface would be a big improvement to Java SE9.
Thanks for reading,
- Philipp Bibik.
Not quite. You don't have the ability to specify a particular implementation (e.g. the thing won't work if what you want is, say, LinkedList)
On 9 Jul 2015, at 18:34, kedar mhaswade <kedar.mhaswade@gmail.com> wrote:
Wouldn't Arrays.asList <http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-> work for you?
Pavel, what you can do there is new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5")); On Thu, Jul 9, 2015 at 10:40 AM Pavel Rappo <pavel.rappo@oracle.com> wrote:
Not quite. You don't have the ability to specify a particular implementation (e.g. the thing won't work if what you want is, say, LinkedList)
On 9 Jul 2015, at 18:34, kedar mhaswade <kedar.mhaswade@gmail.com> wrote:
Wouldn't Arrays.asList < http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-
work for you?
Or Stream.of("1", "2", "3").collect(Collectors.toList()); :-) -- daniel On 09/07/15 19:46, Louis Wasserman wrote:
Pavel, what you can do there is new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5"));
On Thu, Jul 9, 2015 at 10:40 AM Pavel Rappo <pavel.rappo@oracle.com> wrote:
Not quite. You don't have the ability to specify a particular implementation (e.g. the thing won't work if what you want is, say, LinkedList)
On 9 Jul 2015, at 18:34, kedar mhaswade <kedar.mhaswade@gmail.com> wrote:
Wouldn't Arrays.asList < http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-
work for you?
Yeah, or LinkedList<String> strings = new LinkedList<>(Arrays.asList("1", "2", "3")); But I guess what Phillip really wants is to be able to *express* ArrayList<String> strings = ["1", "2", "3"]; and I believe there's a reason he can't have it :-( On Thu, Jul 9, 2015 at 10:46 AM, Louis Wasserman <lowasser@google.com> wrote:
Pavel, what you can do there is new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5"));
On Thu, Jul 9, 2015 at 10:40 AM Pavel Rappo <pavel.rappo@oracle.com> wrote:
Not quite. You don't have the ability to specify a particular implementation (e.g. the thing won't work if what you want is, say, LinkedList)
On 9 Jul 2015, at 18:34, kedar mhaswade <kedar.mhaswade@gmail.com> wrote:
Wouldn't Arrays.asList < http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-
work for you?
On 9 Jul 2015, at 18:46, Louis Wasserman <lowasser@google.com> 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. 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
On 07/09/2015 09:05 PM, Pavel Rappo wrote:
On 9 Jul 2015, at 18:46, Louis Wasserman <lowasser@google.com> 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
On 9 Jul 2015, at 20:46, Remi Forax <forax@univ-mlv.fr> wrote:
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.
Thanks Remi, I forgot about that. That's arguably the next best thing after a per-type factory.
On 07/09/2015 01:10 PM, Pavel Rappo wrote:
On 9 Jul 2015, at 20:46, Remi Forax <forax@univ-mlv.fr> wrote:
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. Thanks Remi, I forgot about that. That's arguably the next best thing after a per-type factory.
Would it make sense to have a version that returns the Collection to further simplify the code? Would probably require some work with generics to work nicely. ArrayList<String> l = Collections.addAll(new ArrayList<>(), "1", "2", "3", "4", "5"); //Staffan
Hi Philipp, This area has been the subject of much discussion over the years. Previously we investigated the idea of collection literals in the following research JEP: http://openjdk.java.net/jeps/186 See discussion on lambda-dev: http://mail.openjdk.java.net/pipermail/lambda-dev/2014-January/thread.html#1... We concluded, as a result of that discussion, that it would instead be better to reduce the scope of the problem and focus on convenient factory methods. See the following draft JEP that Stuart is working on: http://openjdk.java.net/jeps/8048330 Paul. On Jul 9, 2015, at 5:55 PM, Philipp Bibik <philipp@bibik.de> wrote:
Hi,
I'm Philipp Bibik and a 16 jear old free time developer. I was born and live in Germany and have only an bad school english, so sorry for any grammar and spelling mistakes.
I have an idea to improve the Java ArrayList API. Lists are one of the most imported and used components in the Java API. Although there are relay well designed, there was always something that annoyed me.
If you want to create an ArrayList with "String" as diamond operator and add the Strings "1" to "5" to them, you need to do something like this:
ArrayList<String> strings = new ArrayList<>(); strings.add("1"); strings.add("2"); strings.add("3"); strings.add("4"); strings.add("5");
I added a few lines of code the the ArrayList class:
/** * @param e array whose elements are to be placed into this list */ @SafeVarargs public ArrayList(E... e) { elementData = e; size = elementData.length; }
After adding the lines above I was able to accomplish the same result with 77 chars / 5 lines less code:
ArrayList<String> strings = new ArrayList<>("1", "2", "3", "4", "5");
In my opinion adding a constructor like this to the "java.util.ArrayList" class and all other (at least some) classes, implementing the "java.util.List" interface would be a big improvement to Java SE9.
Thanks for reading,
- Philipp Bibik.
participants (10)
-
Daniel Fuchs
-
kedar mhaswade
-
Louis Wasserman
-
Martin Buchholz
-
Paul Sandoz
-
Pavel Rappo
-
Philipp Bibik
-
Remi Forax
-
Staffan Friberg
-
Stuart Marks