Reputation: 1141
I have a method that returns Sets of sets. Following is what I mean:
public static HashSet methodName(){
HashSet c= new HashSet();
c.add(x); //x is a HashSet of number
c.add(y); //y is a Hashset of numbers
return c;
}
After the method returns the collections, I enter it into an arraylist
ArrayList<HashSet> xxx= new ArrayList<Hashset>();
y=methodName();
xxx.add(y);
The method gets called couple of times and each time I enter sets of sets into the arraylist.
My question is. now I want to go through the arraylist and find the set that contains the smallest number of sets. How do I do that? Thanks so much in advance. Any help will be appreciated.
Upvotes: 2
Views: 120
Reputation: 39495
A simple way you to go through an array is through an Iterator
. These are utilized in a foreach loop, which can be used when you know the type of elements used in an array (via generics). You can use that in your outer HashSet
- containing ArrayList
:
for (HashSet set : xxx) {
// you need to iterate over the elements in your HashSet here and determine which internal Set has the most elements
for ( Iterator iter = set.iterator(); iter.hasNext();) {
HashSet innerSet = (HashSet) iter.next();
// do the size test
}
}
Upvotes: 0
Reputation: 799380
The size()
method gives the cardinality of a HashSet
. Simply write a Comparator<HashSet>
and use Collections.max()
.
Upvotes: 2