TreeSet(2)

 
/* 因为是二叉树,所以一般查找时间复杂度为 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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值