概述
-
Map集合是雙列集合,一個元素包含兩個值(key和value)
-
Map集合中的元素,key和value資料型別可以相同也可以不同
-
Map集合中的元素,key不允許重復,value可以重復
-
key和value是一一對應的
Map公用方法
public V put(K key, V value):把指定的鍵與指定的值添加到Map集合中,key不重復時,回傳值V為null;key重復的時候,會使用新的value替換Map中的value,回傳被替換的value值
public V remove(Object key):把指定的鍵所對應的鍵值對元素在Map集合中洗掉,回傳被洗掉元素
public V get(Object key):根據指定的鍵,在Map集合中獲取對應的值
boolean containsKey(Object key):判斷集合中是否包含指定的鍵
public Set
keySet():獲取Map集合中所有的鍵,儲存到Set集合中 public Set<Map.Entry<K, V>> entrySet():獲取到Map集合中所有鍵值對物件的集合(Set集合)
遍歷Map集合
-
鍵找值的方法
-
使用Map集合中 keySet()方法將集合中的key取出,儲存到Set集合中
-
利用遍歷器或增強for回圈遍歷Set集合,獲取每一個key
-
使用Map集合中get(Object key)方法,通過key獲取value
public class IteratorDemo01 { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("garen", 100); map.put("temmo", 300); map.put("angel", 100); Set<String> key = map.keySet(); //迭代器的方法 Iterator<String> iterator = key.iterator(); while (iterator.hasNext()){ String next = iterator.next(); Integer value = https://www.cnblogs.com/geqianLee/p/map.get(next); System.out.println(next+"="+value); } System.out.println("================="); //增強for回圈方法 for (String s : map.keySet()) { Integer value = https://www.cnblogs.com/geqianLee/p/map.get(s); System.out.println(s+"="+value); } } }
-
-
鍵值對方法
-
使用Map集合中 entrySet()方法將集合中的Entry物件取出,儲存到Set集合中
-
利用遍歷器或增強for回圈遍歷Set集合,獲取每一個Entry物件
-
使用Entry物件中的方法getKey()和getValue()方法獲取鍵和值
public class IteratorDemo02 { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("garen", 100); map.put("temmo", 300); map.put("angel", 100); Set<Map.Entry<String, Integer>> entry = map.entrySet(); //迭代器方法 Iterator<Map.Entry<String, Integer>> iterator = entry.iterator(); while (iterator.hasNext()){ Map.Entry<String, Integer> next = iterator.next(); String key = next.getKey(); Integer value = https://www.cnblogs.com/geqianLee/p/next.getValue(); System.out.println(key+"="+value); } System.out.println("================="); //增強for回圈方法 for (Map.Entry<String, Integer> m :entry) { String key = m.getKey(); Integer value = https://www.cnblogs.com/geqianLee/p/m.getValue(); System.out.println(key+"="+value); } } }
-
HashMap集合
-
HashMap集合底層是哈希表(陣列+單向鏈表/紅黑樹),查詢速度快
-
HashMap集合是一個無序的集合,存盤元素和取出元素的順序可能不一致
-
HashMap集合存盤自定義鍵值,key元素的hashCode和equals方法需要覆寫重寫
public class Hero { private String name; float hp; public Hero(String name, float hp) { this.name = name; this.hp = hp; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getHp() { return hp; } public void setHp(float hp) { this.hp = hp; } @Override public String toString() { return "Hero{" + "name='" + name + '\'' + ", hp=" + hp + '}'; } //覆寫重寫hashCode和equals方法 @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Hero hero = (Hero) o; return Float.compare(hero.hp, hp) == 0 && Objects.equals(name, hero.name); } @Override public int hashCode() { return Objects.hash(name, hp); } } ///////////////////////////////////////////////////// import java.util.HashMap; import java.util.Map; import java.util.Set; public class HashMapDemo01 { public static void main(String[] args) { HashMap<Hero, String> hashMap = new HashMap<>(); //key不能重復,value可以重復 hashMap.put(new Hero("garen", 100), "Hero01"); hashMap.put(new Hero("temmo", 200), "Hero01"); hashMap.put(new Hero("angel", 300), "Hero02"); hashMap.put(new Hero("garen", 100), "ADHero"); Set<Hero> set = hashMap.keySet(); for (Hero key : set) { String value = https://www.cnblogs.com/geqianLee/p/hashMap.get(key); System.out.println(key+"-->"+value); } System.out.println("============================"); Set<Map.Entry<Hero, String>> entrySet = hashMap.entrySet(); for (Map.Entry<Hero, String> entry : entrySet) { Hero key = entry.getKey(); String value = https://www.cnblogs.com/geqianLee/p/entry.getValue(); System.out.println(key+"-->"+value); } } }
LinkedHashMap集合
- LinkedHashMap集合底層是哈希表+鏈表(保證迭代的順序)
- LinkedHashMap集合是一個有序的集合,存盤元素和取出元素的順序是一致的
靜態方法of
-
List介面,Set介面,Map介面內有靜態方法of,可以給集合一次性添加多個元素
List<E> list = List.of(E...);Set<E> set = Set.of(E...);Map<E> map = Map.of(E...); -
of方法只適用于List介面,Set介面和Map介面,不適用于它們的實作類
-
of方法回傳值是一個不能改變的集合,集合不能再使用add,put等方法添加元素,否則會拋出例外
-
Set介面和Map介面在呼叫of方法時不能有重復的元素,否則會拋出例外
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/153766.html
標籤:Java
上一篇:2020年的六種編程語言排名中,java排第幾只有不到1%的人知道
下一篇:查找--插值查找(Java)
