class Collection3
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
TreeSet<String> ts = new TreeSet<String>(Collections.reverseOrder(new StrLennComparator()));
ts.add("aaa");
ts.add("bbbb");
ts.add("ccccc");
ts.add("dd");
Iterator it = ts.iterator();
while(it.hasNext())
{
sop(it.next());
}
}
}
class StrLennComparator implements Comparator<String>
{
public int compare(String s1, String s2)
{
if(s1.length() > s2.length())
return 1;
if(s1.length() < s2.length())
return -1;
return s1.compareTo(s2);
}
}
java基础入门----Collections.reverseOrder
最新推荐文章于 2024-02-18 09:13:04 发布