Map集合
java.util.Map<k,v>集合
Map集合特点:
- Map集合是一个双列集合,一个元素包含两个值(一个key,一个value)
- Map集合中的元素,key和value的数据类型可以相同,也可以不同
- Map集合中的元素,key是不允许重复的,value是可以重复的
- Map集合中的元素,key和value是一一对应
HashMap集合
java.util.HashMap<k,v>集合 implements Map<k,v>接口
HashMap集合的特点:
- HashMap集合底层是哈希表:查询速度特别快 【JDK1.8之前数组+单向链表 JDK1.8之后数组+单向链表/红黑树(链表的长度超过8);提高查询速度】
- HashMap集合是一个无序集合,存储元素和取出元素的顺序有可能不一致
LinkedHashMap集合
java.util.LinkedHashMap<k,v>集合extends HashMap<k,v>集合
LinkedHashMap集合的特点:
- LinkedHashMap集合底层是哈希表+链表(保证迭代的顺序)
- LinkedHashMap集合是一个有序集合,存储元素和取出元素的顺序是一致的
Map集合方法
public V put(K key, V value): 把指定的键与指定的值添加到Map集合中。
public V remove(Object key): 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值。
public V get(Object key) 根据指定的键,在Map集合中获取对应的值。
boolean containsKey(Object key) 判断集合中是否包含指定的键。
/*
* public V put(K key, V value):把指定的键与指定的值添加到Map集合中。
* 返回值:
* 存储键值对的时候,key不重复,返回值v是null
* 存储键值对的时候,key重复,会使用新的value替换map中重复的value,返回被替换的value值
* */
public static void show1(){
Map<String, String> m = new HashMap<>();
String put1 = m.put("李晨", "范冰冰");
System.out.println(put1);//null
String put2 = m.put("李晨", "范冰冰2");
System.out.println(put2);//范冰冰
System.out.println(m);//{李晨=范冰冰}
System.out.println(m);
}
/*
* public V remove(Object key):把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值
* 返回值:
* key存在,v返回被删除的值
* key不存在,v返回null
* */
public static void show2(){
Map<String, Integer> m = new HashMap<>();
m.put("李白", 170);
m.put("杜甫", 165);
m.put("白居易", 177);
Integer remove = m.remove("李白");
System.out.println(remove);//170
Integer remove2 = m.remove("成龙");
System.out.println(remove2);//null
}
/*
* public V get(Object key)根据指定的键,在Map集合中获取对应的值。
* 返回值:
* key存在,返回对应的value值
* key不存在,返回null
* */
public static void show3(){
Map<String, Integer> m = new HashMap<>();
m.put("李白", 170);
m.put("杜甫", 165);
m.put("白居易", 177);
Integer integer = m.get("李白");
System.out.println(integer);//170
Integer integer2 = m.get("孙尚香");
System.out.println(integer2);//null
}
/*
* boolean containsKey(Object key)判断集合中是否包含指定的键。
* 返回值:
* 包含指定的key返回true 不包含指定的key返回false
* */
public static void show4(){
Map<String, Integer> m = new HashMap<>();
m.put("李白", 170);
m.put("杜甫", 165);
m.put("白居易", 177);
boolean containsKey = m.containsKey("李白");
System.out.println(containsKey);//170
boolean containsKey2 = m.containsKey("孙尚香");
System.out.println(containsKey2);//null
}
Map集合的便利
/*
* Map集合第一种便利方式:通过键找值得方式
* Map集合中的方法:
* 实现步骤:
* 1.使用Map集合中的方法keySet,把Map集合所有的key取出来,存储到一个set集合中
* 2.便利set集合,获取Map集合中的每一个key
* 3.通过Map集合中的方法get(key),通过key找到value
* */
Map<String, Integer> m = new HashMap<>();
m.put("李白", 170);
m.put("杜甫", 165);
m.put("白居易", 177);
//将Map中的key存入到set集合中
Set<String> keySet = m.keySet();
//便利Set集合
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String key = it.next();
Integer integer = m.get(key);//通过便利的每一个key值获取到对应的value
System.out.println(key+"="+integer);
}
//下面使用增强for循环便利
for (String string : keySet) {
Integer integer = m.get(string);
System.out.println(string+" "+integer);
}
}
/*
* Map集合遍历的第二种方式:使用Entry对象遍历
* 实现步骤:
* 1.使用Map集合中的方法entrySet,把Map集合中的多个对象取出来,存储到一个set中
* 2.遍历set集合,获取每一个Entry对象
* 3.使用Entry对象中的getKey和getValue获取键和值
* */
Map<String, Integer> m = new HashMap<>();
m.put("李白", 170);
m.put("杜甫", 165);
m.put("白居易", 177);
//将Map集合中的Entry对象存入到Set集合中去
Set<Entry<String, Integer>> set = m.entrySet();
//使用迭代器遍历 set集合中的每一个entry对象
Iterator<Entry<String, Integer>> it = set.iterator();
while(it.hasNext()){
Entry<String, Integer> next = it.next();
String key = next.getKey();
Integer value = next.getValue();
System.out.println(key+" "+value);
}
System.out.println("============");
//使用增强for循环便利
for (Entry<String, Integer> entry : set) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+" "+value);
}
}
Map存储自定义类型的集合
HashMap集合存储自定义类型健值
Map保证key唯一的:
作为key的元素,必须重写hashCode方法和equals方法,以保证key唯一
/*
* HashMap存储自定义类型键值
* Key:String类型
* String类型重写hashCode方法和equals方法,可以保证key唯一
* value:Person类型
* value可以重复(同名同年龄的人视为同一个)
* */
public static void show1(){
HashMap<String, Person> m = new HashMap<>();
m.put("北京", new Person("张三",20));
m.put("上海", new Person("李四",19));
m.put("广州", new Person("王五",31));
m.put("北京", new Person("孙尚香",20));
//使用keySet遍历
Set<String> keySet = m.keySet();
//增强for循环
for (String key : keySet) {
Person person = m.get(key);
System.out.println(key+" "+person);
}
}
/*
* HashMap存储自定义类型键值
* Key:Person类型
* Person类型就必须重写hashCode方法和equals方法,以保证key唯一
* Value:String
* String类型可以重复
* */
public static void show2(){
HashMap<People, String> m = new HashMap<>();
m.put(new People("张三",20), "北京");
m.put(new People("李四",15), "云南");
m.put(new People("王五",20), "上海");
m.put(new People("张三",20), "越南");
//使用entrySet和增强for循环遍历
Set<Entry<People, String>> set = m.entrySet();
for (Entry<People, String> entry : set) {
People key = entry.getKey();
String value = entry.getValue();
System.out.println(key+" "+value);
}
}
LinkedHashMap集合
java.util.LinkedHashMap<k,v> extends HashMap<k,v>
此集合是存入和取出时有序的
LinkedHashMap<String, String> m = new LinkedHashMap<>();
m.put("a", "b");
m.put("b", "c");
m.put("d", "d");
System.out.println(m);//{a=b, b=c, d=d}
}
Hashtable集合
java.util.Hashtable<k,v> implements Map<k,v>接口
- Hashtable:底层也是一个哈希表,是一个线程安全的集合,是单线程集合,速度慢
- HashMap:底层是一个哈希表,是一个线程不安全的集合,是多线程集合,速度快
- HashMap(之前学的所有的集合):可以存储null值,null键
- Hashtable集合,不能存储null值null键
Hashtable和Vector集合一样,在JDK1.2版本之后被更先进的集合(HashMap,ArrayList)取代了
Hashstable的子类Properties依然活跃在历史的舞台
Properties集合是一个唯一和IO流相结合的集合