你可以提供
Comparator.并使用Collections.sort()
class SizeComarator implements Comparator> {
@Override
public int compare(Set> o1, Set> o2) {
return Integer.valueOf(o1.size()).compareTo(o2.size());
}
}
ArrayList> arrayList = new ArrayList>();
Set set1 = new HashSet();
set1.add("A");
set1.add("B");
Set set2 = new HashSet();
set2.add("A");
arrayList.add(set1);
arrayList.add(set2);
Collections.sort(arrayList, new SizeComarator());
System.out.println(arrayList);
输出:
[[A], [A, B]]