commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jukka Zitting" <jukka.zitt...@gmail.com>
Subject Re: [Collections] "For each pair" iterator
Date Tue, 01 Apr 2008 00:57:54 GMT
Hi,

On Tue, Apr 1, 2008 at 1:25 AM, Pedro Pedruzzi <ppedruzzi@v2com.mobi> wrote:
>  I have a java.util.Set and I'd like to do a iteration like "for each
>  to distinct pair of elements" do something. In this case I don't care
>  for the ordering of the pair. A most precise way would be: "for each
>  subset of size 2" do something.
>
>  Do the Apache Commons Collections provide some class to do this?

I'm not sure if Commons Collections has something like that, but you
should be able to do it quite easily with just a few lines of code:

    Set set = ...;
    Object[] objects = set.toArray();
    for (int i = 0; i < objects.length - 1; i++) {
        for (int j = i + 1; j < objects.length; j++) {
            processPair(objects[i], objects[j]);
        }
    }

BR,

Jukka Zitting

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


Mime
View raw message