New Collection framework?
Thomas Kamps
anubis1 at linux-ecke.de
Mon Feb 8 19:29:56 UTC 2016
Hi everyone,
Did you ever think about a new Collection framework for Java?
My main complains about the java.util Collections are:
* No co/contra variance for assigning. Which forbids assigments like
List<Object> foo = new List<Number>();
Currently it can be bypassed when using wildcards, but then you cannot add
items to foo. Even more problematic are interfaces with some kind of
List<List<...>> as parameter
* The main Collection interface allowes too much. If you get a Collection and
you should not modify it, your programm will either behave stragly with a hard
to detect bug, or crash with some kind of UnsupportedOperationException, when
you do it.
I have my problems with UnsupprtedOperationException, because when i get a
Collection, i have to read the documentation (if existing) to know what i am
allowed to do, or run unexpectedly into an exception. Would it be great to
hava an "readonly" interface, which simply does not have and modifing methods?
The other possible reason could be the Valhalla project, which tries to
introduce value types, to enable Collections with primitive type parameters.
There is (or was?) a discussion how to handle the remove methods on List:
http://mail.openjdk.java.net/pipermail/valhalla-spec-experts/2015-December/000028.html
There is a suggestion for partial methods to solve the problem, but i think
partial methods makes the language even more complicated to compile and
more difficult to understand.
All theese problems could be resolved by just letting the java.util Collection
Framework as it is. And make a new one.
I have created a proposal for it, which can be easily (modulo class names)
used together with the java.util Collections. This is achieved by having
wrapper Collections and converter in both directions.
https://github.com/Blahord/BetterCollections
Note: This is the first release with the target to have a clean and small as
possible API.
Since it makes heavily use of wildcards, it is some kind of bureaucratic.
This could be reduced, by
* Adding a shortcur for wildcards like "?Type" or "? extends Type" and "!Type"
for "? super Type" or
* Allowing co and contra variance for type parameters.
More information about the core-libs-dev
mailing list