我有一个返回集合集的方法。以下是我的意思:
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;
}在该方法返回集合之后,我将其输入到arraylist中
ArrayList<HashSet> xxx= new ArrayList<Hashset>();
y=methodName();
xxx.add(y);这个方法被调用了几次,每次我在arraylist中输入集合的集合。
我的问题是。现在我要遍历数组列表,找到包含最小集合数量的集合。我该怎么做?提前谢谢你了。任何帮助都将不胜感激。
发布于 2010-11-29 11:58:44
浏览数组的一种简单方法是通过Iterator。它们被用在foreach循环中,当您知道数组中使用的元素类型时(通过泛型),就可以使用它。您可以在包含HashSet的外部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
}
}发布于 2010-11-29 11:51:55
size()方法给出了HashSet的基数。只需编写一个Comparator并使用Collections.max()即可。
https://stackoverflow.com/questions/4300699
复制相似问题