【Java類集】_SortedMap類筆記

來源:互聯網
上載者:User

【Java類集】_SortedMap類筆記

本章目標:掌握SortedMap介面的作用

3、具體內容
回顧:SortedSet,是TreeSet的實現介面,那麼此介面可以進行排序的操作。
SortedMap也是排序的操作,之前學習過TreeMap類,那麼此類是可以排序的。

SortedMap介面擴充的方法:
No.    方法                        類型    描述
1    public Comparator<? super K> comparator()    普通    返回比較子對象
2    public K firstKey()                普通    返回第一個元素的key
3    public SoortedMap<K,V> headMap(K toKey)        普通    返回小於等於指定key的部分集合
4    public K lastKey()                普通    返回最後一個元素的key
5    public SortedMap<K,V> subMap(K fromKey,K toKey)    普通    返回指定key範圍的集合
6    public SortedMap<K,V> tailMap(K fromKey)    普通    返回大於指定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>();    //通過子類執行個體化介面對象        map.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());        }    }}

輸出:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.