You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Lee Crawford <cr...@stadeon.com> on 2003/11/24 18:18:41 UTC

/PROPOSAL 1/ Additions to IteratorUtils

I'd like to propose the following simple additions to the
o.a.c.c.IteratorUtils
class. 

The following is a simple convenience method for counting the number of
elements
in an iteration, something that I find myself recreating somewhat
frequently: 

  public static
  int countElements (final Iterator iter) {
    int cnt = 0; 
    while (iter.hasNext ()) { 
      cnt ++; 
    }
    return cnt; 
  }

There isn't an EnumeratorUtils present that might contain an equivalent:


  public static
  int countElements (final Enumerator enum) {
    int cnt = 0; 
    while (enum.hasNext ()) { 
      cnt ++; 
    }
    return cnt; 
  }

Or the slightly less performant: 

  public static
  int countElements (final Enumerator enum) {
    return IteratorUtils.countElements (asIterator (enum)); 
  }

Thoughts? 

--lee



 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] Re: /PROPOSAL 1/ Additions to IteratorUtils

Posted by Stephen Colebourne <sc...@btopenworld.com>.
It seems like an odd thing to want to do. If you need to know the size, hold
the data in a collection.

Stephen

----- Original Message ----- >
In the current CVS HEAD, you can do
>
> IteratorUtils.toArray(Iterator).length
> or
> IteratorUtils.toList(Iterator).size()
>
> But, your suggestion may be a good addition also.
>
> Also, I think that the method definitions you've provided will all cause
> infinite loops -- hasNext() will always return true if you don't call
> next().  We'd need to find a way to copy the iterator so we don't modify
> it's state when we count the elements.
>
> Anyone else have an opinion?
>
>
>
>
> Lee Crawford wrote:
> > I'd like to propose the following simple additions to the
> > o.a.c.c.IteratorUtils
> > class.
> >
> > The following is a simple convenience method for counting the number of
> > elements
> > in an iteration, something that I find myself recreating somewhat
> > frequently:
> >
> >   public static
> >   int countElements (final Iterator iter) {
> >     int cnt = 0;
> >     while (iter.hasNext ()) {
> >       cnt ++;
> >     }
> >     return cnt;
> >   }
> >
> > There isn't an EnumeratorUtils present that might contain an equivalent:
> >
> >
> >   public static
> >   int countElements (final Enumerator enum) {
> >     int cnt = 0;
> >     while (enum.hasNext ()) {
> >       cnt ++;
> >     }
> >     return cnt;
> >   }
> >
> > Or the slightly less performant:
> >
> >   public static
> >   int countElements (final Enumerator enum) {
> >     return IteratorUtils.countElements (asIterator (enum));
> >   }
> >
> > Thoughts?
> >
> > --lee
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


[collections] Re: /PROPOSAL 1/ Additions to IteratorUtils

Posted by __matthewHawthorne <ma...@phreaker.net>.
( Remember to prefix subject with [collections] )

In the current CVS HEAD, you can do

IteratorUtils.toArray(Iterator).length
or
IteratorUtils.toList(Iterator).size()

But, your suggestion may be a good addition also.

Also, I think that the method definitions you've provided will all cause 
infinite loops -- hasNext() will always return true if you don't call 
next().  We'd need to find a way to copy the iterator so we don't modify 
it's state when we count the elements.

Anyone else have an opinion?




Lee Crawford wrote:
> I'd like to propose the following simple additions to the
> o.a.c.c.IteratorUtils
> class. 
> 
> The following is a simple convenience method for counting the number of
> elements
> in an iteration, something that I find myself recreating somewhat
> frequently: 
> 
>   public static
>   int countElements (final Iterator iter) {
>     int cnt = 0; 
>     while (iter.hasNext ()) { 
>       cnt ++; 
>     }
>     return cnt; 
>   }
> 
> There isn't an EnumeratorUtils present that might contain an equivalent:
> 
> 
>   public static
>   int countElements (final Enumerator enum) {
>     int cnt = 0; 
>     while (enum.hasNext ()) { 
>       cnt ++; 
>     }
>     return cnt; 
>   }
> 
> Or the slightly less performant: 
> 
>   public static
>   int countElements (final Enumerator enum) {
>     return IteratorUtils.countElements (asIterator (enum)); 
>   }
> 
> Thoughts? 
> 
> --lee


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org