Map接口
Map提供了一種映射關系,其中的元素是以鍵值對(key-value)的形式存儲的,能夠實現根據key快速查找value;
Map中的鍵值對以Entry類型的對象實例形式存在;
建(key值)不可重復,value值可以重復,一個value值可以和很多key值形成對應關系,每個建最多只能映射到一個值。
Map支持泛型,形式如:Map<K,V>
Map中使用put(K key,V value)方法添加
Map接口中定義的常用方法
具體使用在實現類中討論
int size();//獲取Map集合大小(即元素數量) boolean isEmpty();//判斷是否為空 boolean containsKey(Object key);//判斷是否包含某個鍵 boolean containsValue(Object value);//判斷是否包含某個值 V get(Object key);//獲取某個鍵對應的值 V put(K key, V value);//添加鍵值對(K,V) V remove(Object key);//移除某個鍵對應的鍵值對 void putAll(Map<? extends K, ? extends V> m);//添加另一個Map集合 void clear();//清空所有鍵值對 Set<K> keySet();//獲取鍵的集合 Collection<V> values();//獲取值的集合 Set<Map.Entry<K, V>> entrySet();//獲取鍵值對實體的集合 interface Entry<K,V>//Map中的內部接口
HashMap
基于哈希表的 Map 接口的實現。此實現提供所有可選的映射操作,并允許使用 null 值和 null 鍵。(除了非同步和允許使用 null 之外,HashMap 類與 Hashtable 大致相同。)除實現了Map接口外還實現了Cloneable,Serializable,繼承了AbstractMap抽象類
此類不保證映射的順序,特別是它不保證該順序恒久不變。
特點:
允許鍵為null,值為null
// HashMap<String, Student> hm = new HashMap<String, Student>();
// hm.put("2018050401", new Student("2018050401", "張三", 18, 80.0));
// hm.put("2018050402", new Student("2018050402", "李四", 18, 80.0));
// hm.put("2018050403", new Student("2018050403", "李四", 18, 80.0));
// hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0));
// hm.put("2018050404", new Student("2018050404", "王五", 18, 80.0));
//
// // 方式一: 通過鍵找值
// Set<String> keys = hm.keySet();
// for (String key : keys) {
// Student s = hm.get(key);
// System.out.println(key + "|" + s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore());
// }
HashMap<Student, String> hm = new HashMap<Student, String>();
hm.put(new Student("2018050401", "張三", 18, 80.0),"2018050401");
hm.put(new Student("2018050402", "李四", 18, 80.0),"2018050402");
hm.put(new Student("2018050403", "李四", 18, 80.0), "2018050403");
hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404");
hm.put(new Student("2018050404", "王五", 18, 80.0), "2018050404");
// 方式二: 通過鍵值對對象找鍵找值
Set<Entry<Student, String>> keyValues = hm.entrySet();
for (Entry<Student, String> keyValue : keyValues) {
Student s = keyValue.getKey();
String value = keyValue.getValue();
System.out.println(s.getId() + "|" + s.getName() + "|" + s.getAge() + "|" + s.getScore() + "=" + value);
}
LinkedHashMap
Map 接口的哈希表和鏈表實現,具有可預知的迭代順序
特點:
底層數據結構是哈希表和鏈表,哈希表保證鍵唯一,鏈表保證鍵有序
LinkedHashMap<Integer, String> lhm = new LinkedHashMap<Integer, String>();
lhm.put(01, "張三1");
lhm.put(02, "張三2");
lhm.put(03, "張三3");
lhm.put(04, "張三4");
lhm.put(05, "張三5");
Set<Integer> keys = lhm.keySet();
for (Integer key : keys) {
System.out.println(key + "|" + lhm.get(key));
}
TreeMap
基于紅黑樹(Red-Black tree)的 NavigableMap 實現。該映射根據其鍵的自然順序進行排序,或者根據創建映射時提供的 Comparator 進行排序,
具體取決于使用的構造方法。
特點:
排序方式類似于TreeSet,分為自然排序和比較器排序,具體取決于使用的構造方法
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(24, "Hello1");
tm.put(14, "Hello2");
tm.put(34, "Hello3");
tm.put(124, "Hello4");
tm.put(24, "Hello5");
tm.put(24, "Hello6");
tm.put(24, "Hello7");
tm.put(244, "Hello8");
tm.put(624, "Hello9");
tm.put(24, "Hello10");
Set<Integer> keys = tm.keySet();
for (Integer key : keys) {
String value = tm.get(key);
System.out.println(key + "|" + value);
}
HashTable
此類實現一個哈希表,該哈希表將鍵映射到相應的值。任何非 null 對象都可以用作鍵或值
特點:
HashMap和Hashtable的區別:
底層都是哈希表結構
Hashtable<String, String> hashtable = new Hashtable<String, String>();
hashtable.put("劉備", "孫尚香");
hashtable.put("孫策", "大喬");
hashtable.put("周瑜", "小喬");
hashtable.put("呂布", "貂蟬");
System.out.println(hashtable);
Enumeration<String> keys = hashtable.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
String value = hashtable.get(key);
System.out.println(key + "|" + value);
}
WeakHashMap
以弱鍵 實現的基于哈希表的 Map。在 WeakHashMap 中,當某個鍵不再正常使用時,將自動移除其條目。更精確地說,對于一個給定的鍵,其映射的存在并不阻止垃圾回收器對該鍵的丟棄,這就使該鍵成為可終止的,被終止,然后被回收。
丟棄某個鍵時,其條目從映射中有效地移除,因此,該類的行為與其他的 Map 實現有所不同。
WeakHashMap<String,String> whm = new WeakHashMap<>();
whm.put(new String("hello1"), "world1");
whm.put(new String("hello2"), "world2");
whm.put(new String("hello3"), "world3");
whm.put("hello4", "world3");
System.out.println(whm);
System.gc();
System.runFinalization();
System.out.println(whm);
鍵是枚舉類型
EnumMap<Direction, String> em = new EnumMap<>(Direction.class);
em.put(Direction.UP, "向上移動");
em.put(Direction.DOWN, "向下移動");
em.put(Direction.LEFT, "向左移動");
em.put(Direction.RIGHT, "向右移動");
Set<Direction> keys = em.keySet();
for (Direction key : keys) {
String value = em.get(key);
System.out.println(key + "|" + value);
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。