class GenericDemo2
{
public static void main(String[] args)
{
TreeSet<String> ts = new TreeSet<String> (new StringLengthComparator());
ts.add("abcd");
ts.add("cc");
ts.add("cba");
ts.add("aaa");
ts.add("z");
ts.add("hahaha");
for(Iterator<String> it = ts.iterator(); it.hasNext() ;)
{
String s = it.next();
System.out.println(s);
}
}
}
class StringLengthComparator implements Comparator<String>
{
public int compare(String o1 , String o2)
{
int num = new Integer(o1.length()).compareTo
(new Integer(o2.length()));
if(num == 0)
return o1.compareTo(o2);
return num;
}
}