/*
因为是二叉树,所以一般查找时间复杂度为 o(lg(n)),
这个效率当然没有HashMap的效率高.
不过TreeMap比HashMap功能强大,
如果不需要排序的话当然不会用TreeMap,
如果需要排序的话,HashMap无法胜任,当然要用TreeMap了,它可以求子Map.
所以这个是适用场合问题,无法比较他们.
*/
import java.util. * ;
class TreeSetTest2
{
public static void main(String[] args)
{
TreeSet < TreeSetTest2 > ts = new TreeSet < TreeSetTest2 > ( new TreeSetTest2Comparator() );
Scanner sc = new Scanner(System.in);
int quit;
System.out.println( " input numbers, to quit,input 0 " );
while ((quit = sc.nextInt()) != 0 )
ts.add( new TreeSetTest2(quit) );
for ( TreeSetTest2 tst : ts)
System.out.println( tst.getInt() );
}
int i;
TreeSetTest2( int i){
this .i = i;
}
int getInt(){
return i;
}
}
class TreeSetTest2Comparator implements Comparator < TreeSetTest2 > // 这里不用泛型的话编译会有警告
{
/* public int compare(Object object1, Object object2){
return -(((TreeSetTest2)object1).i - ((TreeSetTest2)object2).i) ;
}
*/
public int compare(TreeSetTest2 tst1,TreeSetTest2 tst2){
return - (tst2.i - tst1.i);
}
}
这个效率当然没有HashMap的效率高.
不过TreeMap比HashMap功能强大,
如果不需要排序的话当然不会用TreeMap,
如果需要排序的话,HashMap无法胜任,当然要用TreeMap了,它可以求子Map.
所以这个是适用场合问题,无法比较他们.
*/
import java.util. * ;
class TreeSetTest2
{
public static void main(String[] args)
{
TreeSet < TreeSetTest2 > ts = new TreeSet < TreeSetTest2 > ( new TreeSetTest2Comparator() );
Scanner sc = new Scanner(System.in);
int quit;
System.out.println( " input numbers, to quit,input 0 " );
while ((quit = sc.nextInt()) != 0 )
ts.add( new TreeSetTest2(quit) );
for ( TreeSetTest2 tst : ts)
System.out.println( tst.getInt() );
}
int i;
TreeSetTest2( int i){
this .i = i;
}
int getInt(){
return i;
}
}
class TreeSetTest2Comparator implements Comparator < TreeSetTest2 > // 这里不用泛型的话编译会有警告
{
/* public int compare(Object object1, Object object2){
return -(((TreeSetTest2)object1).i - ((TreeSetTest2)object2).i) ;
}
*/
public int compare(TreeSetTest2 tst1,TreeSetTest2 tst2){
return - (tst2.i - tst1.i);
}
}