本篇文章給大家?guī)砹岁P(guān)于java的相關(guān)知識,其中主要介紹了關(guān)于Map集合體系的基本使用以及常用API的相關(guān)內(nèi)容,下面一起來看一下,希望對大家有幫助。
Map集合概述和使用
Map集合是一種雙列集合,每個元素包含兩個數(shù)據(jù)。
Map集合的每個元素的格式:key=value(鍵值對元素)。
Map集合也被稱為“鍵值對集合”。
Map集合整體格式:
Collection集合的格式:
[元素1,元素2,元素3..]
Map集合的完整格式:
{key1=value1 , key2=value2 , key3=value3 , ...}
Map集合的使用場景之一:購物車系統(tǒng)
分析:
購物車提供的四個商品和購買的數(shù)量在后臺需要容器存儲。
每個商品對象都一一對應(yīng)一個購買數(shù)量。
把商品對象看成是Map集合的建,購買數(shù)量看成Map集合的值。
例如:
{商品1=2 , 商品2=3 , 商品3 = 2 , 商品4= 3}
Map集合體系的特點
Map集合中使用最多的Map集合是HashMap。
重點掌握HashMap , LinkedHashMap , TreeMap。其他的后續(xù)理解。
Map集合體系特點:
Map集合的特點都是由鍵決定的。
Map集合的鍵是無序,不重復(fù)的,無索引的,值不做要求(可以重復(fù))。
Map集合后面重復(fù)的鍵對應(yīng)的值會覆蓋前面重復(fù)鍵的值。
Map集合的鍵值對都可以為null。
Map集合實現(xiàn)類特點:
HashMap:元素按照鍵是無序,不重復(fù),無索引,值不做要求。(與Map體系一致)
public static void main(String[] args) { // 創(chuàng)建一個HashMap對象 Map<String, Integer> maps = new HashMap<>(); // 向集合添加元素 maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); // 鍵一樣會覆蓋前面的 maps.put(null, null); // 鍵值對可以為null // 輸出集合, 可以發(fā)現(xiàn)是無序的 System.out.println(maps); // {null=null, 凳子=10, 桌子=10}}登錄后復(fù)制LinkedHashMap:元素按照鍵是有序,不重復(fù),無索引,值不做要求。
public static void main(String[] args) { // 創(chuàng)建一個LinkedHashMap對象 // Map<String, Integer> maps = new HashMap<>(); Map<String, Integer> maps = new LinkedHashMap<>(); // 向集合添加元素 maps.put("桌子", 2); maps.put("凳子", 10); maps.put("桌子", 10); // 鍵一樣會覆蓋前面的 maps.put(null, null); // 鍵值對可以為null // 輸出集合, 是有序的 System.out.println(maps); // {桌子=10, 凳子=10, null=null}}登錄后復(fù)制TreeMap:元素是按照鍵排序,不重復(fù),無索引的,值不做要求。
public static void main(String[] args) { // 創(chuàng)建一個HashMap對象 // Map<String, Integer> maps = new HashMap<>(); // Map<String, Integer> maps = new LinkedHashMap<>(); Map<String, Integer> maps = new TreeMap<>(); // 向集合添加元素 maps.put("ddd", 2); maps.put("bbb", 10); maps.put("ddd", 3); maps.put("aaa", 5); maps.put("ccc", 1); // 輸出集合, 元素按照鍵進行排序 System.out.println(maps); // {aaa=5, bbb=10, ccc=1, ddd=3}}登錄后復(fù)制
Map集合常用的API
Map集合:
Map是雙列集合的祖宗接口,它的功能是全部雙列集合都可以繼承使用的。
Map API如下:
方法名稱 | 說明 |
---|---|
put(K key,V value) | 添加元素 |
remove(Object key) | 根據(jù)鍵, 刪除鍵值對元素 |
clear() | 移除所有的鍵值對元素 |
containsKey(Object key) | 判斷集合是否包含指定的鍵 |
containsValue(Object value) | 判斷集合是否包含指定的值 |
isEmpty() | 判斷集合是否為空 |
size() | 集合的長度,也就是集合中鍵值對的個數(shù) |
put方法添加元素
public static void main(String[] args) { // 創(chuàng)建Map集合對象 Map<String, Integer> maps = new HashMap<>(); // 添加元素 maps.put("華為", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); System.out.println(maps); // {iPhone=6, 生活用品=15, 華為=10, 小米=5}}
remove方法, 根據(jù)鍵刪除元素
public static void main(String[] args) { // 創(chuàng)建Map集合對象 Map<String, Integer> maps = new HashMap<>(); // 添加元素 maps.put("華為", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 刪除元素 maps.remove("小米"); System.out.println(maps); // {iPhone=6, 生活用品=15, 華為=10}}
clear方法, 清空集合元素
public static void main(String[] args) { // 創(chuàng)建Map集合對象 Map<String, Integer> maps = new HashMap<>(); // 添加元素 maps.put("華為", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 清空元素 maps.clear(); System.out.println(maps); // {}}
containsKey()方法, 判斷是否包含指定鍵
public static void main(String[] args) { // 創(chuàng)建Map集合對象 Map<String, Integer> maps = new HashMap<>(); // 添加元素 maps.put("華為", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判斷是否包含指定鍵 System.out.println(maps.containsKey("華為")); // true System.out.println(maps.containsKey("魅族")); // false}
containsValue方法, 判斷是否包含指定值
public static void main(String[] args) { // 創(chuàng)建Map集合對象 Map<String, Integer> maps = new HashMap<>(); // 添加元素 maps.put("華為", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判斷是否包含指定值 System.out.println(maps.containsValue(6)); // true System.out.println(maps.containsValue(99)); // false}
isEmpty, 判斷集合是否為空
public static void main(String[] args) { // 創(chuàng)建Map集合對象 Map<String, Integer> maps = new HashMap<>(); // 添加元素 maps.put("華為", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 判斷集合是否為空 System.out.println(maps.isEmpty()); // false}
size方法, 集合元素的個數(shù)
public static void main(String[] args) { // 創(chuàng)建Map集合對象 Map<String, Integer> maps = new HashMap<>(); // 添加元素 maps.put("華為", 10); maps.put("小米", 5); maps.put("iPhone", 6); maps.put("生活用品", 15); // 返回集合元素的個數(shù) System.out.println(maps.size()); // 4}
擴展方法: putAll合并其他集合, 合并遇到重復(fù)的key會進行合并
public static void main(String[] args) { Map<String, Integer> map1 = new HashMap<>(); map1.put("java", 1); map1.put("C語言", 2); Map<String, Integer> map2 = new HashMap<>(); map2.put("python", 4); map2.put("linux", 7); // 合并兩個集合 map1.putAll(map2); System.out.println(map1); // {{python=4, java=7, C語言=2}}
推薦學(xué)習(xí):《java視頻教程》