import java.util.*;
class GenericDemo2
{
public static void main(String[] args)
{
//优先自定义比较器,
TreeSet<String> ts = new TreeSet<String>(new LenComparator());
ts.add("abcd");
ts.add("d");
ts.add("acd");
ts.add("abjkiyd");
ts.add("abcjkld");
Iterator<String> it = ts.iterator();
while(it.hasNext())
{
String s = it.next();
System.out.println(s+"---"+s.length());
}
}
}
class LenComparator implements Comparator<String>
{
public int compare(String o1,String o2)
{
int num = new Integer(o2.length()).compareTo((new Integer(o1.length())));
if(num==0)
return o1.compareTo(o2);
return num;
}
}
泛型实例2
最新推荐文章于 2022-08-22 11:53:12 发布