-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
5.0, 7
A DESCRIPTION OF THE REQUEST :
I propose, for most of the Collection implementations (eg. HashSet, TreeSet, LinkedList, ArrayList, etc) something like:
class HashSet<E> ... {
public HashSet(E item, E ... items) {
add(item);
for (E e : items)
add(e);
}
...
}
JUSTIFICATION :
It's the best kind of syntactic sugar. It's not too sweet and doesn't require any new syntax. Collections often have only one or a few members, and they are kind of annoying to set up. You can either do things verbosely (and therefore error-pronely) like this:
class Something {
public static final Set<Something> someSomethings = new HashSet<Something>();
static {
someSomethings.add(Something.THING0);
someSomethings.add(Something.THING1);
}
}
...or you end up with kludges like in EnumSet<E>:
public static <E extends Enum<E>> EnumSet<E> of(E e)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e4, E e5)
That is begging for varargification. Granted, those are not constructors, but that's just an implementation detail of EnumSet. Sets in general (and indeed, most Collections) would benefit from a varargs constructor.
###@###.### 2005-03-24 19:14:25 GMT
I propose, for most of the Collection implementations (eg. HashSet, TreeSet, LinkedList, ArrayList, etc) something like:
class HashSet<E> ... {
public HashSet(E item, E ... items) {
add(item);
for (E e : items)
add(e);
}
...
}
JUSTIFICATION :
It's the best kind of syntactic sugar. It's not too sweet and doesn't require any new syntax. Collections often have only one or a few members, and they are kind of annoying to set up. You can either do things verbosely (and therefore error-pronely) like this:
class Something {
public static final Set<Something> someSomethings = new HashSet<Something>();
static {
someSomethings.add(Something.THING0);
someSomethings.add(Something.THING1);
}
}
...or you end up with kludges like in EnumSet<E>:
public static <E extends Enum<E>> EnumSet<E> of(E e)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3)
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e4, E e5)
That is begging for varargification. Granted, those are not constructors, but that's just an implementation detail of EnumSet. Sets in general (and indeed, most Collections) would benefit from a varargs constructor.
###@###.### 2005-03-24 19:14:25 GMT
- duplicates
-
JDK-7075432 Add constructor with elipse argument
- Closed
-
JDK-8048330 JEP 269: Convenience Factory Methods for Collections
- Closed
- relates to
-
JDK-8297283 Improve Unsupported exception messages for java.util.AbstractList
- Open