commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J. Moldawski (JIRA)" <>
Subject [jira] [Created] (COLLECTIONS-450) Iterate over the all elements excluding the last/first one
Date Sat, 16 Mar 2013 22:30:13 GMT
J. Moldawski created COLLECTIONS-450:

             Summary: Iterate over the all elements excluding the last/first one
                 Key: COLLECTIONS-450
             Project: Commons Collections
          Issue Type: Wish
          Components: Collection
            Reporter: J. Moldawski
            Priority: Minor

The Problem
In many applications you will extremly often find this sort of code:
int i=0;
for (element:elements)
  if (i!=elemets.size)
     // Just for last element
It happens often, if not just all collections's elements themself must be processed, but some
actions must be performed on going from one element to the next. Since the last element has
no successor, this actions must be skipped when processing the last element. A very famous
example is, if your are going the generate a comma-separated-vector from a Collection<String>:
You will end up in a code like above.

The method <T>CollectionUtils.forAllButLastDo(Collection<T>, C)
should be introduced, which process all elements of a collection, but skips the last one,
which will be just returned. The above code can be then re-written:

   new Closure<T>{
      execute(T element){

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see:

View raw message