溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java中Map的簡介及其使用

發(fā)布時間:2021-08-25 16:59:40 來源:億速云 閱讀:128 作者:chen 欄目:編程語言

這篇文章主要講解了“Java中Map的簡介及其使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java中Map的簡介及其使用”吧!

1.Map集合概述和特點(diǎn)

概述:
將鍵映射到值的對象,一個映射不能包含重復(fù)的鍵,每個鍵最多只能映射到一個值。
Map接口和Collection接口的不同
Map是雙列的,Collection是單列的
Map的鍵唯一,Collection的子體系Set是唯一的
Map集合的數(shù)據(jù)結(jié)構(gòu)針對鍵有效,跟值無關(guān);Collection集合的數(shù)據(jù)結(jié)構(gòu)是針對元素有效。

2.Map集合的功能概述

(1):添加
V put(K key,V value):添加元素。這個其實還有另一個功能?替換
如果鍵是第一次存儲,就直接存儲元素,返回null
如果鍵不是第一次存在,就用值把以前的值替換掉,返回以前的值
(2)  :刪除
void clear():移除所有的鍵值對元素
V remove(Object key):根據(jù)鍵刪除鍵值對元素,并把值返回
(3)  :判斷
boolean containsKey(Object key):判斷集合是否包含指定的鍵
boolean containsValue(Object value):判斷集合是否包含指定的值
boolean isEmpty():判斷集合是否為空
(4)  :獲取
Set<Map.Entry<K,V>> entrySet(): 返回一個鍵值對的Set集合
V get(Object key):根據(jù)鍵獲取值
Set keySet():獲取集合中所有鍵的集合
Collection values():獲取集合中所有值的集合
(5)  :長度
int size():返回集合中的鍵值對的對數(shù)

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

獲取所有鍵的集合,遍歷鍵的集合,獲取到每一個鍵根據(jù)鍵找值  

示例代碼如下:

public class Test4 {
   public static void main(String[] args) {
       HashMap<Phone,String> map = new HashMap<>();
       map.put(new Phone("Apple",7000),"美國");
       map.put(new Phone("Sony",5000),"日本");
       map.put(new Phone("Huawei",6000),"中國");
       Set<Phone> phones = map.keySet();
       Iterator<Phone> iterator = phones.iterator();
       while (iterator.hasNext()){
           Phone next = iterator.next();
           System.out.println(next.getBrand()+"=="+next.getPrice()+"=="+map.get(next));
       }

   }
}
class Phone{
   private String Brand;
   private int Price;

   public Phone(String brand, int price) {
       Brand = brand;
       Price = price;
   }

   public String getBrand() {
       return Brand;
   }

   public void setBrand(String brand) {
       Brand = brand;
   }

   public int getPrice() {
       return Price;
   }

   public void setPrice(int price) {
       Price = price;
   }
}

獲取所有鍵值對對象的集合,遍歷鍵值對對象的集合,獲取到每一個鍵值對對象,根據(jù)鍵值對對象找鍵和值  

示例代碼如下:

public class Test4 {
   public static void main(String[] args) {
       HashMap<Phone,String> map = new HashMap<>();
       map.put(new Phone("Apple",7000),"美國");
       map.put(new Phone("Sony",5000),"日本");
       map.put(new Phone("Huawei",6000),"中國");
       Set<Map.Entry<Phone, String>> entries = map.entrySet();
       for (Map.Entry<Phone, String> entry : entries) {
           System.out.println(entry.getKey().getBrand()+"==="+entry.getKey().getPrice()+"==="+entry.getValue());
       }
   }
}

4.LinkedHashMap的概述和使用

LinkedHashMap的概述: Map 接口的哈希表和鏈接列表實現(xiàn),具有可預(yù)知的迭代順序。LinkedHashMap的特點(diǎn): 底層的數(shù)據(jù)結(jié)構(gòu)是鏈表和哈希表 元素有序 并且唯一。
元素的有序性由鏈表數(shù)據(jù)結(jié)構(gòu)保證 唯一性由 哈希表數(shù)據(jù)結(jié)構(gòu)保證。
Map集合的數(shù)據(jù)結(jié)構(gòu)只和鍵有關(guān)  

5.TreeMap集合

TreeMap 鍵不允許插入null
TreeMap: 鍵的數(shù)據(jù)結(jié)構(gòu)是紅黑樹,可保證鍵的排序和唯一性
排序分為自然排序和比較器排序
線程是不安全的效率比較高
6.TreeMap集合排序:
實現(xiàn)Comparable接口,重寫CompareTo方法
使用比較器  

7.TreeMap集合的遍歷

示例代碼如下:

public class Test4 {
   public static void main(String[] args) {
       TreeMap<Phone,String> map = new TreeMap<>();
       map.put(new Phone("Apple",7000),"美國");
       map.put(new Phone("Sony",5000),"日本");
       map.put(new Phone("Huawei",6000),"中國");
       Set<Phone> phones = map.keySet();
       Iterator<Phone> iterator = phones.iterator();
       while(iterator.hasNext()){
           Phone next = iterator.next();
           System.out.println(next.getBrand()+"==="+next.getPrice()+"==="+map.get(next));
       }
   }
}
class Phone implements Comparable<Phone>{
   private String Brand;
   private int Price;

   public Phone(String brand, int price) {
       Brand = brand;
       Price = price;
   }

   public String getBrand() {
       return Brand;
   }

   public void setBrand(String brand) {
       Brand = brand;
   }

   public int getPrice() {
       return Price;
   }

   public void setPrice(int price) {
       Price = price;
   }

   @Override
   public int compareTo(Phone o) {
       return this.getPrice() - o.getPrice();
   }
}

8.Collections工具類的概述和常見方法

(1):Collections類概述: 針對集合操作 的工具類
(2):Collections成員方法
public static void sort(List list): 排序,默認(rèn)按照自然順序
public static int binarySearch(List<?> list,T key): 二分查找
public static T max(Collection<?> coll): 獲取最大值
public static void reverse(List<?> list): 反轉(zhuǎn)
public static void shuffle(List<?> list): 隨機(jī)置換

Map中的鍵唯一,但是當(dāng)存儲自定義對象時,需要重寫Hashcode和equals方法  

感謝各位的閱讀,以上就是“Java中Map的簡介及其使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Java中Map的簡介及其使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI