Map官方檔案
Map
- 關于Map的說明
- 關于Map.Entry
關于Map的說明
Map是一個介面類,該類沒有繼承自Collection,該類中存盤的是<K,V>結構的鍵值對,并且K一定是唯一的,不能重復,
關于Map.Entry<K, V>的說明
Map.Entry<K, V> 是Map內部實作的用來存放<key, value>鍵值對映射關系的內部類,該內部類中主要提供了<key, value>的獲取,value的設定以及Key的比較方式,

注意:Map.Entry<K,V>并沒有提供設定Key的方法
Map 的常用方法說明

注意:
1. Map是一個介面,不能直接實體化物件,如果要實體化物件只能實體化其實作類TreeMap或者HashMap
2. Map中存放鍵值對的Key是唯一的,value是可以重復的
3. 在Map中插入鍵值對時,key不能為空,否則就會拋NullPointerException例外,但是value可以為空
4. Map中的Key可以全部分離出來,存盤到Set中來進行訪問(因為Key不能重復),
5. Map中的value可以全部分離出來,存盤在Collection的任何一個子集合中(value可能有重復),
6. Map中鍵值對的Key不能直接修改,value可以修改,如果要修改key,只能先將該key洗掉掉,然后再來進行重新插入,
7. TreeMap和HashMap的區別

TreeMap:
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* @author 枳洛淮南
* @version 1.0
* @Description map 的使用
* @Date 2021/4/2 下午 15:57
*/
public class mapDemo
{
public static void main(String[] args)
{
Map<String, Integer> mailList = new TreeMap<>();
mailList.put("火警", 119);
mailList.put("警察", 110);
mailList.put("天氣", 114);
mailList.put("查號", 114);
//回傳所有 key,set 會自動去重
Set<String> keys = mailList.keySet();
System.out.println(keys + "\n");
//回傳所有 value
Collection<Integer> values = mailList.values();
System.out.println(values + "\n");
//回傳鍵值對
Set<Map.Entry<String, Integer>> entries = mailList.entrySet();
System.out.println(entries + "\n");
//是否含有該 key
boolean flag = mailList.containsKey("火警");
System.out.println(flag);
flag = mailList.containsKey("匪警");
System.out.println(flag + "\n");
//是否含有該 value
flag = mailList.containsValue(114);
System.out.println(flag);
flag = mailList.containsValue(222);
System.out.println(flag);
}
public static void main1(String[] args)
{
Map<String, Integer> mailList = new TreeMap<>();
mailList.put("ss", 888);
//put 方法回傳插入之前該 key 映射的值
Integer v = mailList.put("sh", 123);
System.out.println(v);
System.out.println(mailList);
v = mailList.put("sh", 456);
System.out.println(v);
System.out.println(mailList);
System.out.println();
//get 獲取某 key 對應的值,如果沒有該 key ,回傳null
v = mailList.get("sh");
System.out.println(v);
v = mailList.get("sh");
System.out.println(v);
v = mailList.get("jj");
System.out.println(v);
System.out.println();
//getOrDefault(key value) 獲取某 key 對應的值,如果沒有該 key ,回傳默認的 value
//不會修改集合本身
System.out.println(mailList);
v = mailList.getOrDefault("jj", -1);
System.out.println(v);
System.out.println(mailList);
System.out.println();
//remove(key) 洗掉key - value 映射關系,回傳value,沒有 key 則回傳 null
v = mailList.remove("sh");
System.out.println(v);
System.out.println(mailList);
System.out.println();
}
}
自己實作的TreeMap:
/**
* @author 枳洛淮南
* @version 1.0
* @Description 功能
* @Date 2021/4/2 下午 16:14
*/
public class MyTreeMap
{
private TreeNode root;
@Override
public String toString()
{
return "MyTreeMap{" +
"root=" + root +
'}';
}
public Integer put(String key, Integer value)
{
if (root == null)
{
root = new TreeNode(key, value);
return null;
}
TreeNode cur = root;
TreeNode parent = null;
while (cur != null)
{
int cmp = key.compareTo(cur.key);
if (0 == cmp)
{
Integer oldValue = cur.value;
cur.value = value;
return oldValue;
} else if (cmp < 0)
{
parent = cur;
cur = cur.left;
} else
{
parent = cur;
cur = cur.right;
}
}
TreeNode node = new TreeNode(key, value);
int cmp = key.compareTo(parent.key);
if (cmp < 0)
{
parent.left = node;
} else
{
parent.right = node;
}
return null;
}
public Integer get(String key)
{
TreeNode cur = root;
while (cur != null)
{
int cmp = key.compareTo(cur.key);
if (0 == cmp)
{
return cur.value;
} else if (cmp < 0)
{
cur = cur.left;
} else
{
cur = cur.right;
}
}
return null;
}
public Integer getOrDefault(String key, Integer defaultValue)
{
Integer v = get(key);
if (v != null)
{
return v;
}
return defaultValue;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/272224.html
標籤:java
上一篇:JAVA初窺-DAY02
下一篇:你能區分這幾種Java集合型別?
