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

(coll) foreach loop should use RandomAccess marker interface when possible rather than Iterable

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 5.0
    • core-libs

      The compiler may choose foreach loop implementation
      with respect to declared collection type.

      I proposed the following rule :
      if collection is a List that implements RandomAccess,
      use indexed loop rather than iterator loop
      else use Iterable interface.

      In order to convince you, this paragraph is extracted from
      RandomAccess documentation :

      A List implementation should implement this interface if, for typical
      instances of the class, this loop:

             for (int i=0, n=list.size(); i < n; i++)
                 list.get(i);


      runs faster than this loop:

             for (Iterator i=list.iterator(); i.hasNext(); )
                 i.next();

            Unassigned Unassigned
            jdriscol Jerry Driscoll (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: