Map集合介紹 java
阿新 • • 發佈:2018-12-15
Map集合概述和特點
- Map介面特點(鍵值對)
- 將鍵對映到值的物件
- 一個對映不能包含重複的鍵(多對一)鍵可多而值不可多
- 每個鍵最多隻能對映到一個值
- Map介面和Collection介面的不同
- Map是雙列的,Collection是單列的
- Map的鍵唯一,Collection的子體系Set是唯一的
- Map集合的資料結構值針對鍵有效,跟值無關;
Collection集合的資料結構是針對元素有效
依賴關係
我們可以看下hashSet和HashMap的依賴關係。
hashSet的add方法
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
HashMap的put方法
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || ( n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
結論:hashSet呼叫了HashMap方法。
Map集合的功能——增刪功能
增加
//增加 put()方法
Map<String,Integer> map = new HashMap<>();
Integer i1 = map.put("xiaoming",20);//返回Null
Integer i2 = map.put("xiaohong",21);
Integer i3 = map.put("xiaoqiang",22);
Integer i4 = map.put("xiaoming",22); //如果之前有了就返回被覆蓋值
//特點:如果鍵是第一次儲存,就直接儲存元素,並返回null,不是的話,就用值替換掉,返回以前的值
刪除
//刪除 clear() 移除所有的鍵值對元素
//remove(Object key):根據鍵刪除鍵值對元素,並把值返回
```java
Integer value = map.remove("xiaoming");//刪除元素,返回鍵對應的值
判斷
- boolean containsKey(Object key):判斷集合是否包含指定的鍵
- boolean containsValue(Object value):判斷集合是否包含指定的值
- boolean isEmpty():判斷集合是否為空
獲取 - Set<Map.Entry<K,V>> entrySet():
- V get(Object key):根據鍵獲取值
- Set keySet():獲取集合中所有鍵的集合,
- Collection values():獲取集合中所有值的集合
長度 - int size():返回集合中的鍵值對的個數
Map集合的遍歷之鍵找值
沒有迭代器,所以不能用迭代器iterator
方法一:
獲取所有鍵的集合
遍歷鍵的集合,獲取到每一個鍵
根據鍵找值
HashMap<String, Integer> hm = new HashMap<>();
hm.put("張三", 23);
hm.put("李四", 24);
hm.put("王五", 25);
hm.put("趙六", 26);
Set<String> keySet = hm.keySet(); //獲取集合中所有的鍵
Iterator<String> it = keySet.iterator(); //獲取迭代器
while(it.hasNext()) { //判斷單列集合中是否有元素
String key = it.next(); //獲取集合中的每一個元素,其實就是雙列集合中的鍵
Integer value = hm.get(key); //根據鍵獲取值
System.out.println(key + "=" + value); //列印鍵值對
}
//等同於
for(String key : hm.keySet()) { //增強for迴圈迭代雙列集合第一種方式
System.out.println(key + "=" + hm.get(key));
}
方法二:
Map.Entry物件
獲取所有鍵值對物件的集合
遍歷鍵值對物件的集合,獲取到每一個鍵值對物件
根據鍵值對物件找鍵和值
Set<Map.Entry<String,Integer>> entryDet = map.entrySet();
//獲取每個物件
Iterator<Map.Entry<String,Integer>> it = entry.iterator();
while(it.hasNext()){
Map.Entry<String,Integer> en = it.next();
String key = en.getKey();
Integer value = en.getValue();
System.out.println(key + "="+value);
}
//等同於
for(Map.Entrt<String,Integer> en : map.entrySet()){
String key = en.getKey();
Integer value = en.getValue();
System.out.println(key + "="+value);
}
LinkedHashMap
LinkedHashMap和HashMap的特點是一樣的,不過多一個特點是,保證如何存就如何取,那麼查詢就肯定慢一點
TreeMap集合鍵
TreeMap<String,Integer> tm = new TreeMap<>();
public static void main(String[] args) {
//demo1();
TreeMap<Student, String> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getName().compareTo(s2.getName()); //按照姓名比較
return num == 0 ? s1.getAge() - s2.getAge() : num;
}
});
tm.put(new Student("張三", 23), "北京");
tm.put(new Student("李四", 13), "上海");
tm.put(new Student("趙六", 43), "深圳");
tm.put(new Student("王五", 33), "廣州");
System.out.println(tm);
}