Map<K,V>集合的特点:
K用来限制键的类型,V用来限制值的类型
1.Map集合存储元素是以键值对的形式存储,每一个键值对都有键和值
2.Map集合的键是唯一,值可以重复,如果键重复了,那么值就会被覆盖
3.根据键取值
Map集合子类:
- HashMap<K,V>:存储数据采用的哈希表结构,元素的存取顺序不能保证一致。
由于要保证键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。
- LinkedHashMap<K,V>:HashMap下有个子类LinkedHashMap,存储数据采用的哈希表结构+链表结构。
通过链表结构可以保证键值对的存取顺序一致;
通过哈希表结构可以保证的键的唯一、不重复,需要重写键的hashCode()方法、equals()方法。
- TreeMap<K,V>:TreeMap集合和Map相比没有特有的功能,底层的数据结构是红黑树;
可以对元素的键进行排序,排序方式有两种:自然排序和比较器排序。
Map<K,V>的常用方法:
- public V put(K key, V value): 把指定的键与指定的值添加到Map集合中。
- public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
- public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
- public boolean containsKey(Object key):判断该集合中是否有此键
- public boolean containsValue(Object value):判断该集合中是否有此值
- public Set<K> keySet(): 获取Map集合中所有的键,存储到Set集合中。
- public Collection<V> values(): 获取Map集合中所有的值,存储到Collection集合中。
- public Set<Map.Entry<K,V>> entrySet(): 获取到Map集合中所有的键值对对象的集合(Set集合)。
Map.Entry<K,V>:表示键值对对象---把键值对包装成一个对象,该对象的类型就是Entry类型
public class Test {
public static void main(String[] args) {
// 创建Map集合,限制键的类型为String,值的类型为String
Map<String, String> map = new HashMap<>();
// 往map集合中添加键值对
map.put("湖人", "科比");
map.put("公牛", "乔丹");
map.put("骑士", "詹姆斯");
System.out.println(map);// {公牛=乔丹, 骑士=詹姆斯, 湖人=科比}
// Map集合键唯一,如果键重复了,值会覆盖
String v1 = map.put("公牛", "皮蓬");
System.out.println("v1:"+v1);// 乔丹
System.out.println(map);// {公牛=皮蓬, 骑士=詹姆斯, 湖人=科比}
// Map集合值可以重复
String v2 = map.put("热火", "詹姆斯");
System.out.println("v2:"+v2);// null
System.out.println(map);// {公牛=皮蓬, 骑士=詹姆斯, 热火=詹姆斯, 湖人=科比}
// 删除公牛这个键对应的键值对
String v3 = map.remove("公牛");
System.out.println("被删除键值对的值:"+v3);// 皮蓬
System.out.println(map);// {骑士=詹姆斯, 热火=詹姆斯, 湖人=科比}
// 获取湖人这个键对应的值
String value = map.get("湖人");
System.out.println("value:"+value);// 科比
// 判断是否包含指定的键
System.out.println(map.containsKey("湖人"));// true
System.out.println(map.containsKey("公牛"));// false
// 判断是否包含指定的值
System.out.println(map.containsValue("科比"));// true
System.out.println(map.containsValue("乔丹"));// false
// 获取所有的键
Set<String> keys = map.keySet();
System.out.println("keys:"+keys);// [骑士, 热火, 湖人]
// 获取所有的值
Collection<String> values = map.values();
System.out.println("values:"+values);// [詹姆斯, 詹姆斯, 科比]
// 获取Map集合中所有键值对对象
Set<Map.Entry<String, String>> set = map.entrySet();
System.out.println(set);// [骑士=詹姆斯, 热火=詹姆斯, 湖人=科比]
}
}
Map的遍历
方式1:键找值方式
通过元素中的键,获取键所对应的值
分析步骤:
1. 获取Map中所有的键,由于键是唯一的,所以返回一个Set集合存储所有的键。方法提示:`keyset()`
2. 遍历键的Set集合,得到每一个键。
3. 根据键,获取键所对应的值。方法提示:`get(K key)`
public class Demo {
public static void main(String[] args) {
// 创建Map集合对象,限制键的类型为String,值的类型为String
Map<String, String> map = new HashMap<>();
// 往map集合中添加键值对
map.put("湖人", "科比");
map.put("公牛", "乔丹");
map.put("骑士", "詹姆斯");
// 遍历map集合
// 获取集合中所有的键 Set<K> keySet()方法
Set<String> keys = map.keySet();
// 遍历所有的键的集合
for (String key : keys) {
// 在循环中,根据键找值 V get(K key)方法
String value = map.get(key);
System.out.println("键:"+key+",值:"+value);
}
}
}
方式2:键值对方式
Entry<K,V>接口:简称Entry项,表示键值对对象,用来封装Map集合中的键值对
Entry<K,V>接口:是Map接口中的内部接口,在外部使用的时候是这样表示: Map.Entry<K,V>
Map集合中提供了一个方法来获取所有键值对对象:
public Set<Map.Entry<K,V>> entrySet()
根据键值对对对象获取键和值:
- public K getKey():获取Entry对象中的键。
- public V getValue():获取Entry对象中的值。
Map遍历方式二:根据键值对对象的方式
1.获取集合中所有键值对对象,以Set集合形式返回。 Set<Map.Entry<K,V>> entrySet()
2.遍历所有键值对对象的集合,得到每一个键值对(Entry)对象。
3.在循环中,可以使用键值对对对象获取键和值 getKey()和getValue()
public class Demo {
public static void main(String[] args) {
// 创建Map集合对象,限制键的类型为String,值的类型为String
Map<String, String> map = new HashMap<>();
// 往map集合中添加键值对
map.put("湖人", "科比");
map.put("公牛", "乔丹");
map.put("骑士", "詹姆斯");
// 获取集合中所有键值对对象 Set<Map.Entry<K,V>> entrySet()
Set<Map.Entry<String, String>> entrySet = map.entrySet();
// 遍历所有键值对对象的集合
for (Map.Entry<String, String> entry : entrySet) {
// 在循环中,可以使用键值对对对象获取键和值 getKey()和getValue()
String key = entry.getKey();
String value = entry.getValue();
System.out.println("键:"+key+",值:"+value);
}
}
}
public class Test {
public static void main(String[] args) {
/*
Map集合练习:输入一个字符串,统计该字符串中每个字符出现次数。
*/
// 0.输入一个字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine();
// 1.创建Map集合,键的类型为Character,值的类型为Integer
HashMap<Character, Integer> map = new HashMap<>();
// 2.循环遍历字符串
for (int i = 0; i < s.length(); i++) {
// 3.在循环中获取遍历出来的字符
char c = s.charAt(i);
// 4.判断集合中是否存在该字符的键
if (map.containsKey(c)) {
// 6.如果集合中已存在该字符的键,那么就取出该字符键对应的值,然后自增1,作为新的值,重新存储到Map集合中
Integer value = map.get(c);
value++;
map.put(c, value);
} else {
// 5.如果集合中不存在该字符的键,那么就该字符作为键,值为1,存储到Map集合中
map.put(c, 1);
}
}
// 7.循环结束,打印map集合
System.out.println(map);
}
}