臨沂恒商做網(wǎng)站成都網(wǎng)站建設軟件
目錄
- 1. Map和Set
- 2. Map的使用
- 3. Set的使用
1. Map和Set
Java中,Map和Set是兩個接口,TreeSet、HashSet這兩個類實現(xiàn)了Set接口,TreeMap、HashMap這兩個類實現(xiàn)了Map接口。
帶Tree的這兩個類(TreeSet、TreeMap)底層的數(shù)據(jù)結(jié)構(gòu)是一棵紅黑樹(一棵特殊的二叉搜索樹),帶Map的這兩個類(HashSet、HashMap)底層的數(shù)據(jù)結(jié)構(gòu)是哈系桶
2. Map的使用
Map中常用的方法
方法 | 作用 |
---|---|
V get(Object Key) | 返回key的value |
V getOrDefault(Object key,V defaultValue) | 返回key的value,key不存在則返回默認值 |
V put(K key,V value) | 設置key對應的value/插入一個新的鍵值對 |
V remove(Object key) | 刪除key對應的映射關(guān)系 |
Set< K > keySet() | 返回所有的key |
Collection< V > values() | 返回所有的value |
Set<Map.Entry<K,V>> entrySet() | 返回 |
boolean containKey(Object key) | 判斷是否包含key |
boolean containValue(Object value) | 判斷是否包含value |
Map.Entry<K,V>中的方法
方法 | 說明 |
---|---|
K getKey() | 返回key |
V getValue() | 返回Entry的value |
V setValue(V value) | 將原來的value替換為指定的value |
舉個例子~~
public static void main(String[] args) {Map<Integer, String> map = new TreeMap<>();//new HashMap也是一樣的map.put(1, "ZhangSan");map.put(2, "LiSi");map.put(3, "WangWu");//獲取所有的key,返回值是Set<K>Set<Integer> set = map.keySet();System.out.println("獲取所有的key:" + set);System.out.println("-------------");//獲取所有的valuesCollection<String> collections = map.values();System.out.println("獲取所有的value" + collections);System.out.println("-------------");//獲取所有的key和valuesSet<Map.Entry<Integer, String>> entries = map.entrySet();System.out.println("獲取所有的key和value" + entries);System.out.println("-------------");//key不能重復,value可以重復System.out.println("使用Map.Entry<Integer, String>中的setValue替換前");//for (Map.Entry<Integer, String> entry : entries) {System.out.println(entry.getKey() + " " + entry.getValue());}for (Map.Entry<Integer, String> entry : entries) {entry.setValue("111111");}System.out.println("替換后");for (Map.Entry<Integer, String> entry : entries) {System.out.println(entry.getKey() + " " + entry.getValue());}
}
輸出結(jié)果:
注意事項:
- 1、Map存儲的是Key-Value結(jié)構(gòu)的鍵值對,key是唯一的不能重復,value可以重復
- 2、插入新的鍵值對時,如果key重復了,會更新key對應的value的值
- 3、TreeMap插入的鍵值對,key不能為空,value可以為空;HashMap插入的鍵值對key和value都可以為空
- 4、Map中的key想要修改,只能先刪除,再重新插入
3. Set的使用
與Map不同的是,Set只存儲key,不存儲value
常用的方法
方法 | 說明 |
---|---|
boolean add() | 添加元素,重復的元素不會添加成功 |
void clear() | 清空整個集合 |
boolean contains(Object o) | 判斷o是否在集合中 |
Iterator< E > iterator() | 迭代器 |
boolean remove(Object o) | 刪除集合中的o |
int size() | 返回set中的元素個數(shù) |
boolean isEmpty() | 判斷是否為空 |
Object[] toArray() | 將set中的元素轉(zhuǎn)換為數(shù)組 |
例
public static void main(String[] args) {Set<Integer> set = new TreeSet<>();set.add(1);set.add(2);set.add(3);set.add(4);set.add(5);System.out.println(set);System.out.println(set.size());System.out.println(set.contains(6));System.out.println(set.contains(5));Object[] arr = set.toArray();System.out.println("-------------");for (Object o : arr) {System.out.print(o + " ");}System.out.println();System.out.println("---------------");Iterator<Integer> iterator = set.iterator();//迭代器,用于遍歷setwhile (iterator.hasNext()) {System.out.print(iterator.next() + " ");}
}
輸出結(jié)果:
注意事項:
- 1、set只存儲了key值,并且key是唯一的,不能重復
- 2、TreeSet的底層是使用Map來實現(xiàn)的,插入key時,value會默認插入一個Object對象
- 3、TreeSet不能插入null,HashSet可以插入null