TreeSet (2)

Source: Internet
Author: User

/* Because it is a binary tree, the general search time complexity is O (lg (n )),
Of course, this efficiency is not as efficient as hashmap.
However, treemap is more powerful than hashmap,
If no sorting is required, treemap is not used,
If sorting is required, hashmap cannot be competent. Of course, it must use treemap. It can be used as a sub-map.
Therefore, this is an applicable issue and cannot be compared.
*/
Import java. util .*;
Class treesettest2
{
Public static void main (string [] ARGs)
{
Treeset <treesettest2> TS = new treeset <treesettest2> (New treesettest2comparator ());
Pipeline SC = new pipeline (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> // if you do not use generics, a warning will be reported during compilation.
{
/* 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 );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.