commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Pedro Pedruzzi" <ppedru...@v2com.mobi>
Subject Re: [Collections] "For each pair" iterator
Date Wed, 02 Apr 2008 21:39:57 GMT
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 <agile.aspect@gmail.com> 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: user-unsubscribe@commons.apache.org
>  For additional commands, e-mail: user-help@commons.apache.org
>
>

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


Mime
View raw message