Map
Map与List、Set接口不同,它是由一系列键值对组成的集合,提供了key到Value的映射。同时它也没有继承Collection。在Map中它保证了key与value之间的一一对应关系。也就是说一个key对应一个value,所以它不能存在相同的key值,当然value值可以相同
Map在存储数据的,需要存储两个数据 ,每个数据都是一个集合,这两个集合需要有关联映射 ,存储键值(Key)的集合中的数据是不能重复的,所以存储键的集合我们使用 Set 集合 存储值的集合是允许重复的,所以可以使用list集合
常用实现类 :
HashMap、Hashtable(线程安全) 、TreeMap (可排序的) 、LinkedHashMap (顺序存储)(了解)
学习Map集合 :
- 数据存储
- 从集合中获取数据
- Map集合的遍历
- 各个Map集合的特点
1. HashMap
构造方法
API 方法
public static interface Map.Entry
K getKey()
V getValue()
API方法 | 功能 |
---|---|
public V put(K key, V value) | 向Map集合中添加数据 |
public V get(Object key) | 通过key 键 返回对用的值 |
public Set |
返回Map集合中所有的键 Set集合 |
public boolean containsKey(Object key) | 判断map中是否包含某一个键 |
public boolean containsValue(Object value) | 判断map中是否包含某一个值 |
public V getOrDefault(Object key, V defaultValue) | 通过键获取值,如果获取不到返回一个默认值 |
public boolean isEmpty() | 判断是否为空集合 |
public void putAll(Map<? extends K,? extends V> m) | 把一个map集合中数据存储到一个集合中 |
public V remove(Object key) | 根据键删除某一个值 并返回 |
public V replace(K key, V value) | 替换某一个键对应的值 |
public int size() | 返回集合中的数据的条数 |
public Set |
返回一个Set集合 集合包含键和值 一个对象 |
public Collection |
返回所有的值 Collection集合 |
案例
public class TestMap01 {
public static void main(String[] args) {
Map
maps.put(“WN001”, “张三丰”);
maps.put(“WN002”, “张五级”);
maps.put(“WN003”, “谢逊”);
maps.put(“WN004”, “杨潇”);
maps.put(“WN005”, “成昆”);
maps.put(“WN001”, “阳顶天”);
System.out.println(maps.size());
//Map集合中数据是无序的
System.out.println(maps);
/1. 获取 键值是 WN003 名字
2.获取所有的键值 并且把 键值 循环打印出来
3.获取Map集合中所有的值 并打印出来
4.循环打印出 WN001 —— 阳顶天 遍历map集合
*/
System.out.println(“======================”);
String name = maps.get(“WN003”);
System.out.println(name);
System.out.println(“===================”);
Set
for(String s :keySet) {
System.out.println(s);
}
System.out.println(“=================”);
Collection
for(String s :values){
System.out.println(s);
}
// map集合的第一种遍历方式
System.out.println(“======================”);
for(String k :keySet) {
String v = maps.get(k);
System.out.println(k + “——“+v);
}
System.out.println(“==========================”);
//利用Map.entry对象 遍历map集合 第二种遍历方式
Set
//遍历 set集合
for(Entry
String key = en.getKey();
String value = en.getValue();
System.out.println(key + “============”+value);
}
System.out.println(maps.containsKey(“WN001”)); //true
System.out.println(maps.containsValue(“谢逊”)); // true
}
}
2. Hashtable (了解)
hashtable 的用法和HashMap 完全一样 ,区别 :线程安全 、效率低
3.TreeMap
Map集合的键 的存储使用的是Set集合 ,Set集合的特点无序的,所以Map集合中的数据也是没有顺序的。
为了给Map集合进行排序 ,JDK提供了另外一种 Map集合 叫 TreeMap
为了使用TreeMap ,那么我买单键 ,必须实现比较器接口 或者 创建TreeMap 对象时 在构造方法中提供比较器
构造方法
PAI 方法
public class TestTreeMap {
public static void main(String[] args) {
TreeMap
maps.put(“a”, “北京”);
maps.put(“c”, “重庆”);
maps.put(“t”, “天津”);
maps.put(“b”, “上海”);
maps.put(“g”, “苏州”);
maps.put(“d”, “东京”);
//默认升序
for(Entry
System.out.println(en.getKey() + “——“ + en.getValue());
}
//降序
TreeMap
@Override
public int compare(String o1, String o2) {
// TODO Auto-generated method stub
return o2.compareTo(o1);
}
});
citys.put(“a”, “北京”);
citys.put(“c”, “重庆”);
citys.put(“t”, “天津”);
citys.put(“b”, “上海”);
citys.put(“g”, “苏州”);
citys.put(“d”, “东京”);
//降序排列
for(Entry
System.out.println(en.getKey() + “=====” + en.getValue());
}
}
}
4. LinkedHashMap (了解)
存储数据时 ,是有序的 。
public class TestLinkedHashMap {
public static void main(String[] args) {
//存储键的时候 ,按照存储的顺序 进行存储的
LinkedHashMap
maps.put(1, “AAA”);
maps.put(3, “BBB”);
maps.put(6, “CCC”);
maps.put(45, “DDD”);
maps.put(2, “EEE”);
maps.put(4, “FFF”);
maps.put(24, “GGG”);
Set
for(int i : keySet){
System.out.println(i);
}
}
}