[Collections] "For each pair" iterator

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[Collections] "For each pair" iterator

Pedro Pedruzzi
Hi guys,

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?

Thanks in advance,

--
Pedro Pedruzzi | V2COM | +55 11 3094-3939

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Collections] "For each pair" iterator

Jukka Zitting
Hi,

On Tue, Apr 1, 2008 at 1:25 AM, Pedro Pedruzzi <[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[Collections] "For each pair" iterator

Pedro Pedruzzi
In reply to this post by Pedro Pedruzzi
Hi guys,

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?

Thanks in advance,

--
Pedro Pedruzzi | V2COM | +55 11 3094-3939

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [Collections] "For each pair" iterator

GUPTA GAURAV KUMAR
In reply to this post by Pedro Pedruzzi
Hi

Please provide more details about your query. Better to give some
example.

KR
Gaurav Kumar

-----Original Message-----
From: Pedro Pedruzzi [mailto:[hidden email]]
Sent: Tuesday, April 01, 2008 3:56 AM
To: [hidden email]
Subject: [Collections] "For each pair" iterator

Hi guys,

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?

Thanks in advance,

--
Pedro Pedruzzi | V2COM | +55 11 3094-3939

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Collections] "For each pair" iterator

Pedro Pedruzzi
Hi,

What I'd like to do is iterate through each subset of size 2 of a Set.

Example:

My set is: S = {1,2,3,4}
I'd like to iterate throught every one of these subsets:
{1,2}, {1,3}, {1,4}, {2,3}, {2,4} and {3,4}.


--
Pedro Pedruzzi |  V2COM | +55 11 3094-3939


On Mon, Mar 31, 2008 at 11:59 PM, GUPTA GAURAV KUMAR
<[hidden email]> wrote:

> Hi
>
>  Please provide more details about your query. Better to give some
>  example.
>
>  KR
>  Gaurav Kumar
>
>
>  -----Original Message-----
>  From: Pedro Pedruzzi [mailto:[hidden email]]
>  Sent: Tuesday, April 01, 2008 3:56 AM
>  To: [hidden email]
>  Subject: [Collections] "For each pair" iterator
>
>
>
> Hi guys,
>
>  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?
>
>  Thanks in advance,
>
>  --
>  Pedro Pedruzzi | V2COM | +55 11 3094-3939

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Collections] "For each pair" iterator

James Carman
That's more of a combinatorics problem.  Perhaps math would be
interested in something like that?

On Wed, Apr 2, 2008 at 12:20 PM, Pedro Pedruzzi <[hidden email]> wrote:

> Hi,
>
>  What I'd like to do is iterate through each subset of size 2 of a Set.
>
>  Example:
>
>  My set is: S = {1,2,3,4}
>  I'd like to iterate throught every one of these subsets:
>  {1,2}, {1,3}, {1,4}, {2,3}, {2,4} and {3,4}.
>
>
>
>  --
>  Pedro Pedruzzi |  V2COM | +55 11 3094-3939
>
>
>
>
> On Mon, Mar 31, 2008 at 11:59 PM, GUPTA GAURAV KUMAR
>  <[hidden email]> wrote:
>  > Hi
>  >
>  >  Please provide more details about your query. Better to give some
>  >  example.
>  >
>  >  KR
>  >  Gaurav Kumar
>  >
>  >
>  >  -----Original Message-----
>  >  From: Pedro Pedruzzi [mailto:[hidden email]]
>  >  Sent: Tuesday, April 01, 2008 3:56 AM
>  >  To: [hidden email]
>  >  Subject: [Collections] "For each pair" iterator
>  >
>  >
>  >
>  > Hi guys,
>  >
>  >  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?
>  >
>  >  Thanks in advance,
>  >
>  >  --
>  >  Pedro Pedruzzi | V2COM | +55 11 3094-3939
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [hidden email]
>  For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

RE: [Collections] "For each pair" iterator

wesclark
I believe that is called the "power set".

-----Original Message-----
From: [hidden email] [mailto:[hidden email]]
On Behalf Of James Carman
Sent: Wednesday, April 02, 2008 9:26 AM
To: Jakarta Commons Users List
Subject: Re: [Collections] "For each pair" iterator

That's more of a combinatorics problem.  Perhaps math would be
interested in something like that?

On Wed, Apr 2, 2008 at 12:20 PM, Pedro Pedruzzi <[hidden email]>
wrote:
> Hi,
>
>  What I'd like to do is iterate through each subset of size 2 of a
Set.

>
>  Example:
>
>  My set is: S = {1,2,3,4}
>  I'd like to iterate throught every one of these subsets:
>  {1,2}, {1,3}, {1,4}, {2,3}, {2,4} and {3,4}.
>
>
>
>  --
>  Pedro Pedruzzi |  V2COM | +55 11 3094-3939
>
>
>
>
> On Mon, Mar 31, 2008 at 11:59 PM, GUPTA GAURAV KUMAR  
> <[hidden email]> wrote:
>  > Hi
>  >
>  >  Please provide more details about your query. Better to give some

> >  example.
>  >
>  >  KR
>  >  Gaurav Kumar
>  >
>  >
>  >  -----Original Message-----
>  >  From: Pedro Pedruzzi [mailto:[hidden email]]  >  Sent:
> Tuesday, April 01, 2008 3:56 AM  >  To: [hidden email]  >  
> Subject: [Collections] "For each pair" iterator  >  >  >  > Hi guys,  
> >  >  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?
>  >
>  >  Thanks in advance,
>  >
>  >  --
>  >  Pedro Pedruzzi | V2COM | +55 11 3094-3939
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [hidden email]
>  For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Collections] "For each pair" iterator

James Carman
Close, but a power set is the set of *all* subsets, not just the size-2 subsets.

On Wed, Apr 2, 2008 at 12:27 PM, Wes Clark <[hidden email]> wrote:

> I believe that is called the "power set".
>
>
>
>  -----Original Message-----
>  From: [hidden email] [mailto:[hidden email]]
>  On Behalf Of James Carman
>  Sent: Wednesday, April 02, 2008 9:26 AM
>  To: Jakarta Commons Users List
>  Subject: Re: [Collections] "For each pair" iterator
>
>  That's more of a combinatorics problem.  Perhaps math would be
>  interested in something like that?
>
>  On Wed, Apr 2, 2008 at 12:20 PM, Pedro Pedruzzi <[hidden email]>
>  wrote:
>  > Hi,
>  >
>  >  What I'd like to do is iterate through each subset of size 2 of a
>  Set.
>  >
>  >  Example:
>  >
>  >  My set is: S = {1,2,3,4}
>  >  I'd like to iterate throught every one of these subsets:
>  >  {1,2}, {1,3}, {1,4}, {2,3}, {2,4} and {3,4}.
>  >
>  >
>  >
>  >  --
>  >  Pedro Pedruzzi |  V2COM | +55 11 3094-3939
>  >
>  >
>  >
>  >
>  > On Mon, Mar 31, 2008 at 11:59 PM, GUPTA GAURAV KUMAR
>  > <[hidden email]> wrote:
>  >  > Hi
>  >  >
>  >  >  Please provide more details about your query. Better to give some
>
>  > >  example.
>  >  >
>  >  >  KR
>  >  >  Gaurav Kumar
>  >  >
>  >  >
>  >  >  -----Original Message-----
>  >  >  From: Pedro Pedruzzi [mailto:[hidden email]]  >  Sent:
>  > Tuesday, April 01, 2008 3:56 AM  >  To: [hidden email]  >
>  > Subject: [Collections] "For each pair" iterator  >  >  >  > Hi guys,
>  > >  >  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?
>  >  >
>  >  >  Thanks in advance,
>  >  >
>  >  >  --
>  >  >  Pedro Pedruzzi | V2COM | +55 11 3094-3939
>  >
>  >  ---------------------------------------------------------------------
>  >  To unsubscribe, e-mail: [hidden email]
>  >  For additional commands, e-mail: [hidden email]
>  >
>  >
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [hidden email]
>  For additional commands, e-mail: [hidden email]
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [hidden email]
>  For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Collections] "For each pair" iterator

Agile Aspect
In reply to this post by Pedro Pedruzzi
Hi - here you go.

Note, Set is an interface so I implement it with 3 different concert
classes, namely

    HashSet
    LinkedHashSet
    TreeSet

using generics.

If you're not familiar with generics, then remove all occurences of

    <Integer>

Hope this helps.

---------------------------------------------------------------------------------------------------------
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class MySet {
   
    public static void test(Set<Integer> set, String string){
        set.add(new Integer(1));
        set.add(new Integer(4));
        set.add(new Integer(2));
        set.add(new Integer(3));
        System.out.println(string+":");
        Object[] array=new Object[set.size()];  
        array=set.toArray();
        for(int i=0; i<set.size(); i++) {
            for(int j=0; j<set.size(); j++) {
                if(j > i) {
                    System.out.println(array[i]+","+array[j]);
                }
            }
        }
    }
   
    public static void main(String args[]){
        test(new HashSet<Integer>(),"HashSet");
        test(new TreeSet<Integer>(),"TreeSet");
        test(new LinkedHashSet<Integer>(),"LinkedHashSet");
    }
}

Pedro Pedruzzi wrote:

> Hi,
>
> What I'd like to do is iterate through each subset of size 2 of a Set.
>
> Example:
>
> My set is: S = {1,2,3,4}
> I'd like to iterate throught every one of these subsets:
> {1,2}, {1,3}, {1,4}, {2,3}, {2,4} and {3,4}.
>
>
>  


--
Article. VI. Clause 3 of the constitution of the United States states:

"The Senators and Representatives before mentioned, and the Members of
the several State Legislatures, and all executive and judicial Officers,
both of the United States and of the several States, shall be bound by
Oath or Affirmation, to support this Constitution; but no religious Test
shall ever be required as a Qualification to any Office or public Trust
under the United States."



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [Collections] "For each pair" iterator

Pedro Pedruzzi
Nice.

I implemented in a similar manner:

        public void forEachPair(Set set) {
                Object[] array = set.toArray();
               
                for (int i = 0; i < array.length - 1; i++) {
                        for (int j = i + 1; j < array.length; j++) {
                                // process {array[i], array[j]}
                        }
                }
        }

I was just wondering a optimal implementation from commons collections!

Thanks for your time,

--
Pedro Pedruzzi | V2COM | +55 11 3094-3939


On Wed, Apr 2, 2008 at 6:33 PM, Agile Aspect <[hidden email]> wrote:

> Hi - here you go.
>
>  Note, Set is an interface so I implement it with 3 different concert
>  classes, namely
>
>    HashSet
>    LinkedHashSet
>    TreeSet
>
>  using generics.
>
>  If you're not familiar with generics, then remove all occurences of
>
>    <Integer>
>
>  Hope this helps.
>
>
> ---------------------------------------------------------------------------------------------------------
>  import java.util.HashSet;
>  import java.util.LinkedHashSet;
>  import java.util.Set;
>  import java.util.TreeSet;
>
>  public class MySet {
>      public static void test(Set<Integer> set, String string){
>        set.add(new Integer(1));
>        set.add(new Integer(4));
>        set.add(new Integer(2));
>        set.add(new Integer(3));
>        System.out.println(string+":");
>        Object[] array=new Object[set.size()];         array=set.toArray();
>        for(int i=0; i<set.size(); i++) {
>            for(int j=0; j<set.size(); j++) {
>                if(j > i) {
>                    System.out.println(array[i]+","+array[j]);
>                }
>            }
>        }
>    }
>      public static void main(String args[]){
>        test(new HashSet<Integer>(),"HashSet");
>        test(new TreeSet<Integer>(),"TreeSet");
>        test(new LinkedHashSet<Integer>(),"LinkedHashSet");
>
>    }
>  }
>
>  Pedro Pedruzzi wrote:
>
> > Hi,
> >
> > What I'd like to do is iterate through each subset of size 2 of a Set.
> >
> > Example:
> >
> > My set is: S = {1,2,3,4}
> > I'd like to iterate throught every one of these subsets:
> > {1,2}, {1,3}, {1,4}, {2,3}, {2,4} and {3,4}.
> >
> >
> >
> >
>
>
>  --
>  Article. VI. Clause 3 of the constitution of the United States states:
>  "The Senators and Representatives before mentioned, and the Members of the
> several State Legislatures, and all executive and judicial Officers, both of
> the United States and of the several States, shall be bound by Oath or
> Affirmation, to support this Constitution; but no religious Test shall ever
> be required as a Qualification to any Office or public Trust under the
> United States."
>
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: [hidden email]
>  For additional commands, e-mail: [hidden email]
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]