2010-11-29 3 views
2

집합 집합을 반환하는 메서드가 있습니다. 이 방법은 컬렉션을 반환Java 프로그래밍 도움말

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.

내 질문입니다. 이제 arraylist를 통해 가장 작은 수의 세트를 포함하는 세트를 찾고 싶습니다. 어떻게해야합니까? 미리 감사드립니다. 어떤 도움을 주시면 감사하겠습니다.

답변

0

배열을 통과하는 간단한 방법은 Iterator입니다. 이것은 foreach 루프에서 사용되며 배열에서 사용되는 요소 유형을 알 때 (generics를 통해) 사용할 수 있습니다. 깔끔함에 대한

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 
    } 

} 
+0

전체 코드를 알려주십시오. 크기를 어떻게 비교 하시겠습니까? – sap

+0

'HashSet smallest = null; for (HashSet currentSet : xxx) {if (최소 == null || 가장 작은 크기()> currentSet.size()) 가장 작은 = currentSet;}' – zockman

2

size() 메서드는 HashSet의 카디널리티를 제공합니다. Comparator<HashSet>으로 작성하고 Collections.max()을 사용하십시오.

+0

1 : ArrayList를 포함 - 당신은 당신의 외부 HashSet에 그것을 사용할 수 있습니다. – fastcodejava