[Java class set] _ sortedmap class notes
Objective: To understand the functions of the sortedmap Interface
3. Details
Review: sortedset is the implementation interface of treeset, so this interface can be sorted.
Sortedmap is also a sorting operation. If you have learned the treemap class before, this class can be sorted.
Method for extending the sortedmap interface:
No. method type description
1 Public comparator <? Super K> comparator () Normal Return comparator object
2 Public K firstkey () returns the key of the first element
3 Public soortedmap <K, V> headmap (K Tokey) returns partial sets smaller than or equal to the specified key
4 Public K lastkey () returns the key of the last element
5 Public sortedmap <K, V> submap (K fromkey, K Tokey) returns a set of specified key ranges
6 Public sortedmap <K, V> tailmap (K fromkey) common return part of the set greater than the specified key
Import Java. util. map; import Java. util. sortedmap; import Java. util. treemap; public class sortedmapdemo {public static void main (string ARGs []) {sortedmap <string, string> map = NULL; Map = new treemap <string, string> (); // instantiate the interface object map through subclass. put ("D, jiangker", "www.jiangker.com"); map. put ("C, mldn", "www.mldn.cn"); map. put ("B, mldnjava", "www.mldnjava.cn"); system. out. println (map. firstkey (); system. out. println (map. get (map. firstkey (); system. out. println (map. lastkey (); system. out. println (map. get (map. lastkey (); For (map. entry <string, string> me: map. headmap ("B, mldnjava "). entryset () {system. out. println (Me. getkey () + "-->" + me. getvalue ();} For (map. entry <string, string> me: map. tailmap ("B, mldnjava "). entryset () {system. out. println (Me. getkey () + "-->" + me. getvalue ();} For (map. entry <string, string> me: map. submap ("A, mldn", "C, zhinangtuan "). entryset () {system. out. println (Me. getkey () + "-->" + me. getvalue ());}}}
Output:
B. mldnjava
Www.mldnjava.cn
D. jiangker
Www.jiangker.com
B. mldnjava --> www.mldnjava.cn
C. mldn --> www.mldn.cn
D. jiangker --> www.jiangker.com
B. mldnjava --> www.mldnjava.cn
C. mldn --> www.mldn.cn