1.Map<key,value>集合的特點
- Map集合是一個雙列集合,一個元素包含兩個值(一個key,一個value)
- Map集合中的元素,key和value的數據類型可以相同,也可以不同
- Map集合中的元素,key是不允許重複的,value是可以重複的
- Map集合中的元素,key和value是一一對應的
2.HashMap
(1)HashMap集合的特點:
- HashMap底層是哈希表:查詢的速度特別快:JDK1.8之前:數組+單向鏈表-JDK1.8之後:數組+單向鏈表/紅黑樹(鏈表的長度超過8):提高查詢的速度
- HashMap集合是一個無序的集合,存儲元素和取出元素的順序有可能不一致
3.LinkedHashMap
(1).LinkedHashMap的特點
- LinkedHashMap繼承自HashMap
- LinkedHashMap集合底層是哈希表+鏈表(保證迭代的順序)
- LinkedHashMap集合是一個有序的集合,存儲元素和取出元素的順序是一致的
4.Map中的常用方法
(1)常用方法
public V put(K key,V value); // 把指定的鍵與指定的值添加到Map集合中
public V remove(Object key); //把指定的鍵所對應的鍵值對元素在Map集合中刪除,返回被刪除元素的值
public V get(Object key); //根據指定的鍵,在Map集合中獲取對應的值
boolean containsKey(Object key); //判斷集合中是否包含指定的鍵
public Set<K> keySet(); //獲取Map集合中所有的鍵,存儲到Set集合中
public Set<Map.Entry<K,V>> entrySet(); //獲取到Map集合中所有的鍵值對對象的集合(Set集合)
5.Map.Entry<K,V>:
(1)在Map接口中有一個內部接口Entry
(2)作用:
- 當Map集合一創建,那麼就會在Map集合中創建一個Entry對象,用來記錄鍵與值(鍵值對對象,鍵與值的映射關係)-->結婚證
(3)entrySet():把Map集合內部的多個Entry對象取出來存儲到一個Set集合中
格式
Set<Map.Entry<K,V>> entrySet()
6.Map集合的遍歷方法
(1)Map集合的第一種遍歷方式:通過鍵找值
Map集合中的方法:
Set<K> keySet() 返回此映射中包含的鍵的Set視圖
實現步驟:
1.使用Map集合中的keySet()方法,把Map集合所有的key取出來,存儲到一個Set集合中
2.遍歷Set集合,獲取Map集合中的每一個key
3.通過Map集合中的方法get(key),通過key找到value
示例:
Map02KeySetDemo.java
public class Map02KeySetDemo {
public static void main(String[] args) {
demo01();
}
/**
* 使用keySet方法遍歷Map
*/
private static void demo01() {
Map<String,Integer> map = new HashMap<>();
map.put("林志玲",18);
map.put("吳亦凡",38);
map.put("林志穎",28);
map.put("趙忠祥",48);
map.put("成龍",57);
Set<String> set = map.keySet();
for(String k:set){
Integer value = map.get(k);
System.out.println(k+"="+value);
}
}
}
(2)Map集合的第二種遍歷方式:使用Entry對象遍歷
Map集合中的方法:
Set <Map.Entry<K,V>> entrySet() 返回此映射中包含的映射關係的Set視圖
實現步驟:
1.使用Map集合中的方法entrySet(),把Map集合中多個Entry對象取出來,存儲到一個Set集合中
2.遍歷Set集合,獲取每一個Entry對象
3.視圖Entry對象中的方法getKey()和getValue()獲取鍵和值
示例:
Map03EntrySetDemo.java
public class Map03EntrySetDemo {
public static void main(String[] args) {
demo01();
}
private static void demo01() {
Map<String,Integer> map = new HashMap<>();
map.put("林志玲",18);
map.put("吳亦凡",38);
map.put("林志穎",28);
map.put("趙忠祥",48);
map.put("成龍",57);
Set<Map.Entry<String,Integer>> set = map.entrySet();
System.out.println("========使用迭代器遍歷=========");
Iterator<Map.Entry<String,Integer>> it = set.iterator();
while(it.hasNext()){
Map.Entry<String,Integer> m = it.next();
String K = m.getKey();
Integer V = m.getValue();
System.out.println(K+"="+V);
}
System.out.println("========增強for循環遍歷=========");
for (Map.Entry<String,Integer> m:set) {
String Key = m.getKey();
Integer Value = m.getValue();
System.out.println(Key+"="+Value);
}
}
7.HashMap存儲自定義類型的鍵值
(1)Map集合保證key是唯一的
- 作爲key的元素,必須重寫hashCode方法和equals方法,以保證key唯一
(2)示例
a.HashMap01Demo.java
public class HashMap01Demo {
public static void main(String[] args) {
//demo01();
demo02();
}
/**
* HashMap存儲自定義類型鍵值
* key:Person類型-Person類必須重寫hashCode方法與equals方法,以保證key唯一
* value:String類型-可以重複
*/
private static void demo02() {
HashMap<Person,String> map = new HashMap<>();
map.put(new Person("張三",19),"北京");
map.put(new Person("李四",22),"南京");
map.put(new Person("王五",99),"重慶");
map.put(new Person("趙六",38),"上海");
map.put(new Person("張三",19),"天津");
Set<Map.Entry<Person, String>> entrys = map.entrySet();
for (Map.Entry<Person,String> en:entrys) {
Person person = en.getKey();
String value = en.getValue();
System.out.println(person+"-->"+value);
}
}
/**
* HashMap存儲自定義類型鍵值
* key:String類型 -String類型重寫HashCode和equals方法,可以保證唯一
* value:Person類型 - value可以重複(同名同年齡的人視爲同一個)
*/
private static void demo01() {
HashMap<String,Person> map = new HashMap<>();
map.put("北京",new Person("張三",19));
map.put("南京",new Person("李四",17));
map.put("重慶",new Person("王五",22));
map.put("上海",new Person("趙六",30));
map.put("天津",new Person("胡八",18));
Set<String> set = map.keySet();
for (String s:set) {
Person value = map.get(s);
System.out.println(s+"-->"+value);
}
}
}
b.Person.java
public class Person {
private String name;
private int age;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
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;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
8.LinkedHashMap
(1)LinkedHashMap繼承自HashMap
(2)LinkedHashMap接口的哈希表和鏈表實現,具有可預知的迭代順序
(3)底層原理:哈希表+鏈表(記錄元素的順序)
9.HashTable
(1)HashTable特點
- HashTable繼承自Map<K,V>接口
- HashTable:底層是一個哈希表,是一個線程安全的集合,是單線程集合,速度慢
- HashMap:底層是一個哈希表,是一個線程不安全的集合,是多線程集合,速度快
- HashMap集合:可以存儲null值,null鍵
- HashTable集合:不能存儲null值,null鍵
- HashTable和Vector集合一樣,在jdk1.2版本之後被更先進的集合(HashMap,ArrayList)取代了
- HashTable的子類Properties依然活躍在歷史舞臺
- Properties集合是一個唯一和IO流相結合的集合