Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6245673

(coll) Add a varargs constructor to the various Collection implementations

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 5.0, 7
    • core-libs

      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

            smarks Stuart Marks
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: