Map(嚴(yán)格來(lái)說(shuō)是一個(gè)容器不是一個(gè)集合)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1.Map概述

A:Map接口概述

查看API可以知道:

將鍵映射到值的對(duì)象

一個(gè)映射不能包含重復(fù)的鍵

每個(gè)鍵最多只能映射到一個(gè)值

簡(jiǎn)而言之,一個(gè)key值對(duì)應(yīng)著一個(gè)value,多個(gè)value可以對(duì)應(yīng)著一個(gè)key


B:Map接口和Collection接口的不同

Map是雙列的,Collection是單列的

Map的鍵唯一,Collection的子體系Set是唯一的

Map集合的數(shù)據(jù)結(jié)構(gòu)指針對(duì)鍵有效,跟值無(wú)關(guān);Collection集合的數(shù)據(jù)結(jié)構(gòu)是針對(duì)元素有效

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2.Map的CRUD

A:Map集合的功能概述

a:添加功能

V put(K key,V value):添加元素。

如果鍵是第一次存儲(chǔ),就直接存儲(chǔ)元素,返回null

如果鍵不是第一次存在,就用值把以前的值替換掉,返回以前的值

java public static void add() { Map<String,String> map = new HashMap<>(); map.put("1233", "lisi"); map.put("13", "lisi1"); map.put("134", "lisi2"); map.put("134", "lis"); }

b:刪除功能

void clear():移除所有的鍵值對(duì)元素

V remove(Object key):根據(jù)鍵刪除鍵值對(duì)元素,并把值返回

```java public static void add() { Map<String,String> map = new HashMap<>(); map.put("1233", "lisi"); map.put("13", "lisi1"); map.put("134", "lisi2"); map.put("134", "lis");

? ? map.clear();

? ? String string = map.remove("13");

}

```

c:判斷功能

boolean containsKey(Object key):判斷容器是否包含指定的鍵

boolean containsValue(Object value):判斷容器是否包含指定的值

boolean isEmpty():判斷容器是否為空

```java public static void add() { Map<String,String> map = new HashMap<>(); map.put("1233", "lisi"); map.put("13", "lisi1"); map.put("134", "lisi2"); map.put("134", "lis");

? ? boolean key = map.containsKey("13");

? ? boolean value = map.containsValue("lisi");

? ? System.out.println(key);

? ? System.out.println(value);

}

```

d:獲取功能

Set<Map.Entry<K,V>> entrySet():

```java public static void add() { Map<String,String> map = new HashMap<>(); map.put("1233", "lisi"); map.put("13", "lisi1"); map.put("134", "lisi2"); map.put("134", "lis");

? ? Set<Entry<String,String>> set = map.entrySet();

? ? for (Entry<String, String> entry : set) {

? ? ? ? System.out.println(entry.getKey() + ":" + entry.getValue());

? ? }

} ```

V get(Object key):根據(jù)鍵獲取值

java public static void add() { Map<String,String> map = new HashMap<>(); map.put("1233", "lisi"); map.put("13", "lisi1"); map.put("134", "lisi2"); map.put("134", "lis"); String string = map.get("123"); System.out.println(string); }


Set?keySet():獲取容器中所有鍵的容器

```java public static void add() { Map<String,String> map = new HashMap<>(); map.put("1233", "lisi"); map.put("13", "lisi1"); map.put("134", "lisi2"); map.put("134", "lis");

? ? Set<String> set = map.keySet();

? ? for (String string : set) {

? ? ? ? map.get(string);

? ? }

} ```

Collection?values():獲取容器中所有值的容器

```java public static void add() { Map<String,String> map = new HashMap<>(); map.put("1233", "lisi"); map.put("13", "lisi1"); map.put("134", "lisi2"); map.put("134", "lis");

? ? Collection<String> values = map.values();

}

```

e:長(zhǎng)度功能

int size():返回容器中的鍵值對(duì)的個(gè)數(shù)

3.Map集合的遍歷之鍵找值

A:鍵找值思路:

獲取所有鍵的容器

遍歷鍵的容器,獲取到每一個(gè)鍵

根據(jù)鍵找值

B:案例演示

Map集合的遍歷之鍵找值

```java

public static void add() { Map<String,String> map = new HashMap<>(); map.put("1233", "lisi"); map.put("13", "lisi1"); map.put("134", "lisi2"); map.put("134", "lis");

? ? Set<String> set = map.keySet();

? ? for (String string : set) {

? ? ? ? map.get(string);

? ? }

}

```

?

4.Map集合的遍歷之鍵值對(duì)對(duì)象找鍵和值

A:鍵值對(duì)對(duì)象找鍵和值思路:

獲取所有鍵值對(duì)對(duì)象的容器

遍歷鍵值對(duì)對(duì)象的容器,獲取到每一個(gè)鍵值對(duì)對(duì)象

根據(jù)鍵值對(duì)對(duì)象找鍵和值

B:案例演示

Map集合的遍歷之鍵值對(duì)對(duì)象找鍵和值

? ? public static void add() {

? ? ? ? Map<String,String> map = new HashMap<>();

? ? ? ? map.put("1233", "lisi");

? ? ? ? map.put("13", "lisi1");

? ? ? ? map.put("134", "lisi2");

? ? ? ? map.put("134", "lis");

? ? ? ? Set<Entry<String,String>> set = map.entrySet();

? ? ? ? for (Entry<String, String> entry : set) {

? ? ? ? }

? ? }

5.HashMap集合鍵是String值是Student的案例

A:案例演示

HashMap集合鍵是Student值是String的案例

```java public static void add() { Map<String, Student> map = new HashMap<>(); Student student = new Student(1, "lisi"); map.put(student.getId()+"", student);

} ```

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?6.LinkedHashMap的概述和使用

A:案例演示

LinkedHashMap的特點(diǎn)

底層是鏈表實(shí)現(xiàn)的可以保證怎么存就怎么取

7.TreeMap集合鍵是Student值是String的案例

A:案例演示

TreeMap集合鍵是Student值是String的案例(會(huì)對(duì)key值排序)

8.統(tǒng)計(jì)字符串中每個(gè)字符出現(xiàn)的次數(shù)

A:案例演示

需求:統(tǒng)計(jì)字符串中每個(gè)字符出現(xiàn)的次數(shù)

9.容器嵌套之HashMap嵌套HashMap

A:案例演示

容器嵌套之HashMap嵌套HashMap

10.HashMap和Hashtable的區(qū)別

A:面試題

HashMap和Hashtable的區(qū)別

Hashtable是JDK1.0版本出現(xiàn)的,是線程安全的,效率低,HashMap是JDK1.2版本出現(xiàn)的,是線程不安全的,效率高

Hashtable不可以存儲(chǔ)null鍵和null值,HashMap可以存儲(chǔ)null鍵和null值

B:案例演示

HashMap和Hashtable的區(qū)別

? ? ? ? ? ? ? ? ? ? ? ? ? ??? ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TreeMap:

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容