Sortset class for Java API Learning

Source: Internet
Author: User
Package util; import Java. util. iterator; import Java. util. sortedset; import Java. util. treeset;/***** @ author yjmao * @ deprecated sortedset common method summary * @ version v1.0.0 * @ see these elements are sorted in their natural order, or sort by the comparator that is usually provided when an ordered set is created. */Public class learnsortedset {public static void main (string [] ARGs) {// easymethod (); // hardmethod (); // It is wrong to define it directly, because hashset is unordered, and sortedset is ordered // sortedset <string> Seth = new hashset <string> (); sortedset <string> set = new treeset <string> (); set. add ("AAA"); set. add ("BBB"); set. add ("CCC"); set. add ("DDD"); set. add ("eee"); iterator <string> iterator = set. iterator (); While (iterator. hasnext () {string S = iterator. Next (); system. Err. Print (S + ",") ;}// first (): returns the current first (lowest) element in this set. String Fe = set. first (); system. err. println ("\ n first element:" + Fe); // last (): returns the last (highest) element in the set. String Le = set. last (); system. err. println ("last element:" + le); // headset (): returns some views of this set. Its elements are strictly less than toelement. System. err. print ("the element smaller than CCC (not included) in the Set:"); sortedset <string> heade = set. headset ("CCC"); iterator <string> iteratorh = heade. iterator (); While (iteratorh. hasnext () {string S = iteratorh. next (); system. err. print (S + ",") ;}// tailset (): returns some views of this set. The element is greater than or equal to fromelement. System. err. print ("\ n set contains more elements than CCC (including):"); sortedset <string> taile = set. tailset ("CCC"); iterator <string> iteratore = taile. iterator (); While (iteratore. hasnext () {string S = iteratore. next (); system. err. print (S + ",") ;}// subset (): returns some views of this set, whose elements are from fromelement (included) to toelement (not included ). System. err. elements in the print ("\ n set between CCC (inclusive) and EEE (excluded):"); sortedset <string> sube = set. subset ("CCC", "eee"); iterator <string> iteratort = sube. iterator (); While (iteratort. hasnext () {string S = iteratort. next (); system. err. print (S + ",");}}}

Related Article

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: 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.