Java体系分为两个体系:一个是以Map 接口为延伸;一个是以Collection 接口延伸。
本博客介绍Map接口。
Map 接口向下延伸,分为如下三类:HashMap、HashTable、TreeMap。
1.HashMap(底层键值对:key:value)(线程不安全,效率高)(无序的)
(1)Map 集合遍历的五种方式。
package list;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("1", "中国");
map.put("2", "美国");
map.put("3", "俄罗斯");
map.put("4", "英国");
map.put("5", "法国");
//第一种:通map.KeySet 遍历key 和value,普遍使用,二次取值
for (String key : map.keySet()) {
String value = map.get(key);
System.out.println("key: " + key + " value: " + value);
}
//第二种:通过Map.entrySet 使用iterator 遍历key 和value
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
//第三种:通过Map.entrySet 遍历key 和value 推荐,尤其是容量大时
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}
//第四种:通过map.values()遍历value,无法遍历key
for (String value : map.values()) {
System.out.println(" value: " + value);
}
//第五种:通过map.keySet()key,无法遍历value
for (String key : map.keySet()) {
System.out.println(" key: " + key);
}
}
}
输出结果如下:
key: 1 value: 中国
key: 2 value: 美国
key: 3 value: 俄罗斯
key: 4 value: 英国
key: 5 value: 法国
key: 1 value: 中国
key: 2 value: 美国
key: 3 value: 俄罗斯
key: 4 value: 英国
key: 5 value: 法国
key: 1 value: 中国
key: 2 value: 美国
key: 3 value: 俄罗斯
key: 4 value: 英国
key: 5 value: 法国
value: 中国
value: 美国
value: 俄罗斯
value: 英国
value: 法国
key: 1
key: 2
key: 3
key: 4
key: 5
(2)HashMap 常用方法。
①Hashmap 的存值:
public class HashMapDemo03 {
public static void main(String[] args) {
/// *Integer*/map.put("1", 1);//向map 中添加值(返回这个key 以前的值,如果没有返回null)
HashMap<String, Integer> map = new HashMap<>();
System.out.println(map.put("1", 1));// null
System.out.println(map.put("1", 2));// 1
}
}
② Hashmap 的取值:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("DEMO", 1);
/* Value 的类型 */// 得到map 中key 相对应的value 的值
System.out.println(map.get("1"));// null
System.out.println(map.get("DEMO"));// 1
}
}
③Hashmap 的判断为空:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* boolean */// 判断map 是否为空
System.out.println(map.isEmpty());// true
map.put("DEMO", 1);
System.out.println(map.isEmpty());// false
}
}
④Hashmap 判断是否含有key:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* boolean */// 判断map 中是否存在这个key
System.out.println(map.containsKey("DEMO"));// false
map.put("DEMO", 1);
System.out.println(map.containsKey("DEMO"));// true
}
}
⑤ Hashmap 判断是否含有value:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* boolean */// 判断map 中是否存在这个value
System.out.println(map.containsValue(1));// false
map.put("DEMO", 1);
System.out.println(map.containsValue(1));// true
}
}
⑥Hashmap 删除这个key 值下的value:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* Integer */// 删除key 值下的value
System.out.println(map.remove("1"));// null
map.put("DEMO", 2);
System.out.println(map.remove("DEMO"));// 2(删除的值)
}
}
⑦Hashmap 显示所有的value 值:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* Collection<Integer> */// 显示所有的value 值
System.out.println(map.values());// []
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map.values());// [1, 2]
}
}
⑧Hashmap 的元素个数:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* int */// 显示map 里的值得数量
System.out.println(map.size());// 0
map.put("DEMO1", 1);
System.out.println(map.size());// 1
map.put("DEMO2", 2);
System.out.println(map.size());// 2
}
}
⑨Hashmap 删除这个key 值下的value:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* SET<String> */// 显示map 所有的key
System.out.println(map.keySet());// []
map.put("DEMO1", 1);
System.out.println(map.keySet());// [DEMO1]
map.put("DEMO2", 2);
System.out.println(map.keySet());// [DEMO1, DEMO2]
}
}
⑩Hashmap 显示所有的key 和value:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* SET<map<String,Integer>> */// 显示所有的key 和value
System.out.println(map.entrySet());// []
map.put("DEMO1", 1);
System.out.println(map.entrySet());// [DEMO1=1]
map.put("DEMO2", 2);
System.out.println(map.entrySet());// [DEMO1=1, DEMO2=2]
}
}
⑪Hashmap 添加另一个同一类型的map 下的所有制:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
HashMap<String, Integer> map1 = new HashMap<>();
/* void */// 将同一类型的map 添加到另一个map 中
map1.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);// {DEMO2=2}
map.putAll(map1);
System.out.println(map);// {DEMO1=1, DEMO2=2}
}
}
⑫Hashmap 删除这个key 和value:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* boolean */// 删除这个键值对
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);// {DEMO1=1, DEMO2=2}
System.out.println(map.remove("DEMO2", 1));// false
System.out.println(map.remove("DEMO2", 2));// true
System.out.println(map);// {DEMO1=1}
}
}
⑬Hashmap 替换这个key 的value:(java8)
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* value */// 判断map 中是否存在这个key
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);// {DEMO1=1, DEMO2=2}
System.out.println(map.replace("DEMO2", 1));// 2
System.out.println(map);// {DEMO1=1, DEMO2=1}
}
}
⑭清空这个hashmap:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* void */// 清空map
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map);// {DEMO1=1, DEMO2=2}
map.clear();// 2
System.out.println(map);// {}
}
}
⑮Hashmap 的克隆:
public class HashMapDemo03 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
/* object */// 克隆这个map
map.put("DEMO1", 1);
map.put("DEMO2", 2);
System.out.println(map.clone());// {DEMO1=1, DEMO2=2}
Object clone = map.clone();
System.out.println(clone);// {DEMO1=1, DEMO2=2}
}
}
2.HashTable————(底层键值对:key:value)(线程安全,效率低)(无序的)
详见下一个文档【HashTable 和HashMap 的区别详解】
3.TreeMap————(底层键值对:key:value)(线程不安全,效率高)(有序的)
TreeMap(有序的)使用二叉排序树,插入方法如下,使用二分法插入到应该它在的位置去。需要key
一定要有比较的方法,要么插入comparator 比较器,要么key 对象实现Comparator 接口。
do {
parent = t;
cmp = cpr.compare(key, t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
HashMap 和HashTable 的区别:HashMap 线程不安全,HashTable 线程安全,HashMap 的value 允许为null,HashTable 的Key 和Value 都不允许为null。
TreeMap 中的元素默认按照keys 的自然排序排列。(对Integer 来说,其自然排序就是数字的升序;对String> 来说,其自然排序就是按照字母表排序)。
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, Serializable
构造函数如下:
(1)TreeMap():创建一个空TreeMap,keys 按照自然排序。
TreeMap<Integer, String> treeMap = new TreeMap<>();
(2)TreeMap(Comparator comparator):创建一个空TreeMap,按照指定的comparator排序。
TreeMap<Integer, String> map = new TreeMap<>(Comparator.reverseOrder());
map.put(3, "val");
map.put(2, "val");
map.put(1, "val");
map.put(5, "val");
map.put(4, "val");
System.out.println(map); // {5=val, 4=val, 3=val, 2=val, 1=val}
(3)TreeMap(Map m):由给定的map 创建一个TreeMap,keys 按照自然排序
Map<Integer, String> map = new HashMap<>();
map.put(1, "val");
...
TreeMap<Integer, String> treeMap = new TreeMap<>(map);
(4)TreeMap(SortedMap m):由给定的有序map 创建TreeMap,keys 按照原顺序排序。
常用方法如下:
(1)增添元素
V put(K key, V value):将指定映射放入该TreeMap 中。
V putAll(Map map):将指定map 放入该TreeMap 中。
(2)删除元素
void clear():清空TreeMap 中的所有元素。
V remove(Object key):从TreeMap 中移除指定key 对应的映射。
(3)修改元素
V replace(K key, V value):替换指定key 对应的value 值。
boolean replace(K key, V oldValue, V newValue):当指定key 的对应的value 为指定值时,替换该值为新值。
(4)查找元素
boolean containsKey(Object key):判断该TreeMap 中是否包含指定key 的映射。
boolean containsValue(Object value):判断该TreeMap 中是否包含有关指定value 的映射。
Map.Entry<K, V> firstEntry():返回该TreeMap 的第一个(最小的)映射。
K firstKey():返回该TreeMap 的第一个(最小的)映射的key。
Map.Entry<K, V> lastEntry():返回该TreeMap 的最后一个(最大的)映射。
K lastKey():返回该TreeMap 的最后一个(最大的)映射的key。
v get(K key):返回指定key 对应的value。
SortedMap<K, V> headMap(K toKey):返回该TreeMap 中严格小于指定key 的映射集合。
SortedMap<K, V> subMap(K fromKey, K toKey):返回该TreeMap 中指定范
围的映射集合(大于等于fromKey,小于toKey)
(5)遍历接口
Set<Map<K, V>> entrySet():返回由该TreeMap 中的所有映射组成的Set 对象
void forEach(BiConsumer<? super K,? super V> action):对该TreeMap
中的每一个映射执行指定操作
Collection values():返回由该TreeMap 中所有的values 构成的集合
(6)其他方法
Object clone():返回TreeMap 实例的浅拷贝
Comparator<? super K> comparator():返回给该TreeMap 的keys 排序的
comparator,若为自然排序则返回null
int size():返回该TreepMap 中包含的映射的数量
TreeMap<Integer, String> treeMap = new TreeMap<>();
treeMap.put(1, "a");
treeMap.put(2, "b");
treeMap.put(3, "c");
treeMap.put(4, "d"); // treeMap: {1=a, 2=b, 3=c, 4=d}
treeMap.remove(4); // treeMap: {1=a, 2=b, 3=c}
int sizeOfTreeMap = treeMap.size(); // sizeOfTreeMap: 3
treeMap.replace(2, "e"); // treeMap: {1=a, 2=e, 3=c}
Map.Entry entry = treeMap.firstEntry(); // entry: 1 -> a
Integer key = treeMap.firstKey(); // key: 1
entry = treeMap.lastEntry(); // entry: 3 -> c
key = treeMap.lastKey(); // key: 3
String value = treeMap.get(3); // value: c
SortedMap sortedMap = treeMap.headMap(2); // sortedMap: {1=a}
sortedMap = treeMap.subMap(1, 3); // sortedMap: {1=a, 2=e}
Set setOfEntry = treeMap.entrySet(); // setOfEntry: [1=a, 2=e, 3=c]
Collection<String> values = treeMap.values(); // values: [a, e, c]
treeMap.forEach((integer, s) -> System.out.println(integer + "->" + s));
// output:
// 1 -> a
// 2 -> e
// 3 -> c
遍历方式
(1)for 循环
for (Map.Entry entry : treeMap.entrySet()) {
System.out.println(entry);
}
(2)迭代器循环
Iterator iterator = treeMap.entrySet().iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}