標籤:擷取值 prim entryset getc ati == oid const 重寫
Map介面與Collection不同:
Collection中的集合元素是孤立的,可理解為單身,是一個一個存進去的,稱為單列集合
Map中的集合元素是成對存在的,可理解為夫妻,是一對一對存進去的,稱為雙列集合
Map中存入的是:索引值對,鍵不可以重複,值可以重複
Map介面中的常用集合:
1.HashMap:雜湊表的儲存結構,但是無法保證存取順序
2.LinkedHashMap:儲存資料採用的是雜湊表和鏈表,可以有順序
Map介面的常用方法:
樣本:
package demo;import java.util.HashMap;import java.util.Map;public class MapDemo { public static void main(String[] args) { function1(); function2(); function3(); } public static void function1() { // 將索引值對儲存到集合中 Map<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); map.put("c", 4); System.out.println(map); // {b=2, c=4, a=1} // =串連索引值對,存入重複鍵,則會覆蓋 } public static void function2() { //通過鍵擷取值 Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "a"); map.put(2, "b"); map.put(3, "c"); String value = map.get(1); System.out.println(value);//a //有則傳回值,不存在返回null } public static void function3(){ //移除集合中的索引值對 Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "a"); map.put(2, "b"); map.put(3, "c"); map.remove(3); System.out.println(map); //{1=a, 2=b} }}
Map介面的遍曆:
第一種方式:
package demo;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class MapDemo { public static void main(String[] args) { function1(); function2(); } public static void function1() { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); map.put("d", 4); Set<String> set = map.keySet(); Iterator<String> it = set.iterator(); while (it.hasNext()) { String key = it.next(); Integer value = map.get(key); System.out.println(key + "<==>" + value); } } public static void function2() { Map<String, Integer> map = new HashMap<String, Integer>(); map.put("a", 1); map.put("b", 2); map.put("c", 3); map.put("d", 4); for (String key : map.keySet()) { Integer value = map.get(key); System.out.println(key + "<==>" + value); } }}// 遍曆輸出的無序
第二種方式(根據映射關係):
package demo;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import java.util.Set;public class MapDemo { public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "a"); map.put(2, "b"); map.put(3, "c"); Set<Entry<Integer, String>> set = map.entrySet(); Iterator<Entry<Integer, String>> it = set.iterator(); while (it.hasNext()) { Entry<Integer, String> entry = it.next(); Integer key = entry.getKey(); String value = entry.getValue(); System.out.println(key + "<==>" + value); } }}
儲存自訂對象:
package demo;public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person(String name, int age) { super(); this.name = name; this.age = age; } public Person() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }}
package demo;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;//儲存自訂類型public class HashMapDemo { public static void main(String[] args) { function1(); function2(); } public static void function1() { // 自訂類型作為值出現 HashMap<String, Person> map = new HashMap<String, Person>(); map.put("a", new Person("1", 20)); map.put("b", new Person("2", 20)); map.put("c", new Person("3", 20)); // 利用兩種遍曆 for (String key : map.keySet()) { Person value = map.get(key); System.out.println(key + "<==>" + value); } for (Entry<String, Person> entry : map.entrySet()) { String key = entry.getKey(); Person value = entry.getValue(); System.out.println(key + "<==>" + value); } } public static void function2() { // 自訂類型作為鍵出現 // 保證鍵的唯一性,需要重寫hashcode和equals方法 HashMap<Person, String> map = new HashMap<Person, String>(); map.put(new Person("a", 20), "a"); map.put(new Person("b", 20), "a"); map.put(new Person("c", 20), "a"); map.put(new Person("c", 20), "a"); // 兩種遍曆 for (Person key : map.keySet()) { String value = map.get(key); System.out.println(key + "<==>" + value); } for (Entry<Person, String> entry : map.entrySet()) { System.out.println(entry.getKey() + "<==>" + entry.getValue()); } }}
LinkedHashMap集合:
package demo;import java.util.LinkedHashMap;public class LinkedHashMapDemo { public static void main(String[] args) { LinkedHashMap<String, String> link = new LinkedHashMap<String, String>(); link.put("1", "a"); link.put("2", "a"); link.put("3", "a"); link.put("4", "a"); System.out.println(link); //{1=a, 2=a, 3=a, 4=a} //存取順序一致 }}
set介面下還有一個hashtable集合,但是過時了,現在由hashmap取代
不過,要注意一個問題:
HashMap允許儲存null值,HashTable不允許儲存null值,兩種都不允許儲存null鍵
Java學習筆記32(集合架構六:Map介面)