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

public static <T> Iterable<T> Collections.iterable(Enumeration<T>)

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P5 P5
    • None
    • 7
    • core-libs

      A DESCRIPTION OF THE REQUEST :
      There shall be a factory method in the Collections method allowing to wrap an Enumeration without allocating an ArrayList and copying the complete content.

      An example (outside of Collections, implemented as standalone utility class) can be found here:

      http://headcrashing.eu/code/eu/headcrashing/java/util/Enumerations.java


      JUSTIFICATION :
      Unnecessary consumption of RAM and CPU time.

      As described in this blog entry, the implementation could be five times faster and consume virtually no heap space, when returning a thin Iterable instead of an ArrayList:

      http://weblogs.java.net/blog/mkarg/archive/2010/07/04/using-enumerations-each-statements-five-times-faster-jre-without-ram-l


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      public static final <T> Iterable<T> iterable(final Enumeration<T> enumeration) {
        return new Iterable<T>() {
          public final Iterator<T> iterator() {
            return new Iterator<T>() {
              public final boolean hasNext() {
                return enumeration.hasMoreElements();
              }
              public final T next() {
                return enumeration.nextElement();
              }
              public final void remove() {
                throw new UnsupportedOperationException();
              }
            };
          }
        };
      }
      ACTUAL -
      Collections.list() allocated an ArrayList on the heap that holds a complete copy of the content of the enumeration, what needs time and space.

      ---------- BEGIN SOURCE ----------
      see above
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Download above class from my web site.

            Unassigned Unassigned
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: