1、Map集合
API:
举例:
2、Map 接口的常用方法
常用方法1:
/**
* java.util.Map<k,v>集合
* Map集合的特点:_
* 1.Map集合是一个双列集合, 一个元素包含两个值(一个key,- 个value)
* 2.Map集合中的元素, key和value的数据类型可以相同,也可以不同
* 3.Map集合中的元素, key是不允许重复的,value是可以重复的
* 4.Map集合中的元素, key和value是一一对应
*
* java. util.HashMap<k, v>集合implements Map<k, v>接口
* HashMap集合的特点:
* 1. HashMap集合底层是哈希表:查询的速度特别的快
* JDK1.8之前:数组+单向链表
* JDK1.8之后:数组+单向链表/红黑树(链表的长度超过8) :提高查询的速度
* 2. hashMap集合是一个无序的集合,存储元素和取出元素的顺序有可能不一致
* java.util.LinkedHashMap<k, v>集合extends HashMap<k, v>集合
* LinkedHashMap的特点:
* 1.LinkedHashMap集合底层是哈希表+链表(保证迭代的顺序)
* 2.LinkedHashMap集合是一个有序的集合,存储元素和取出元素的顺序是一致的
*/
public class Map01 {
public static void main(String[] args) {
show01();
show02();
show03();
show04();
}
/**
* boolean containsKey(Object key) 判断集合中是否包含指定的键。
* 包含返回true,不包含返回false
*/
private static void show04() {
System.out.println("-----show04-----");
Map<String,Integer> map = new HashMap<>();
map.put("路飞", 174);
map.put("甚平", 301);
boolean b1 = map.containsKey("甚平");
System.out.println(b1); //true
boolean b2 = map.containsKey("索隆");
System.out.println(b2); //false
}
/**
* public V get(Object key); 根据指定的键,在Map集合中获取对应的值
* 返回值:v
* key存在,v返回value的值
* key不存在,v返回null
*/
private static void show03() {
System.out.println("-----show03-----");
Map<String,Integer> map = new HashMap<>();
map.put("路飞", 174);
map.put("甚平", 301);
Integer v1 = map.get("甚平");
System.out.println("v1--->"+v1); //v1--->301
Integer v2 = map.get("索隆");
System.out.println("v2--->"+v2); //v1--->null
}
/**
* public V remove(Object key); 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除的值
* 返回值:v
* key存在,v返回被删除的值
* key不存在,v返回null
*/
private static void show02() {
System.out.println("-----show02-----");
//创建Map集合对象(多态)
Map<String,Integer> map = new HashMap<>();
map.put("路飞", 174);
map.put("索隆", 181);
map.put("山治", 180);
System.out.println(map); //{山治=180, 甚平=301, 索隆=181, 路飞=174}
Integer v1 = map.remove("山治");
System.out.println("v1--->"+v1); //v1--->180
System.out.println(map); //{索隆=181, 路飞=174}
Integer v2 = map.remove("三治");
System.out.println("v2--->"+v2); //v2--->null
}
/**
* public V put(K key,V value); 把指定的键与指定的值添加到Map集合中。
* 返回值:v
* 存储键值对的时候,key不重复,返回V是null
* 存储键值对的时候,key重复,会使用新的value替换map中重复的value,返回被替换的value
*/
private static void show01() {
System.out.println("-----show01-----");
//创建Map集合对象(多态)
Map<String,String> map = new HashMap<>();
String v1= map.put("李晨", "范冰冰1");
System.out.println("v1--->"+v1); //v1--->null
String v2= map.put("李晨", "范冰冰2");
System.out.println("v2--->"+v2); //v2--->范冰冰1
System.out.println(map); //{李晨=范冰冰2}
System.out.println("-----------------");
map.put("冷锋", "龙小云");
map.put("杨过", "小龙女");
map.put("尹志平", "小龙女");
System.out.println(map);
//{杨过=小龙女, 尹志平=小龙女, 李晨=范冰冰2, 冷锋=龙小云}
}
}
2、Map集合遍历键找值方法
1、Set keySet() :返回此映射中包含的键的 Set 视图。
/**
* Map集合的第一种遍历方式: 通过键找值方法
* Map集合中的方法:
* Set<K> keySet() 返回此映射中包含的键的 Set 视图。
* 实现步骤:
* 1、使用Map集合中的方法keySet(),把Map集合所有的键取出来,存储到一个Set集合中
* 2、遍历Set集合,获取Map集合中的每一个key
* 3、通过Map集合中的方法get(key),通过key找到value
*
*/
public class Map遍历02 {
public static void main(String[] args) {
//创建Map集合对象(多态)
Map<String,Integer> map = new HashMap<>();
map.put("路飞", 174);
map.put("索隆", 181);
map.put("山治", 180);
//1、使用Map集合中的方法keySet(),把Map集合所有的键取出来,存储到一个Set集合中
Set<String> set = map.keySet();
//2、遍历Set集合,获取Map集合中的每一个key
//使用迭代器遍历Set集合
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
//3、通过Map集合中的方法get(key),通过key找到value
Integer value = map.get(key);
System.out.println(key+"--->"+value);
}
System.out.println("--------------------");
//增强for遍历set
for (String key1 : set) {
Integer value1 = map.get(key1);
System.out.println(key1+"--->"+value1);
}
/**
* 山治--->180
* 索隆--->181
* 路飞--->174
*/
}
}
3、Map集合中Entry键值对对象
/**
* Map集合遍历的第二种方式:使用Entry对象遍历
* Map集合中的方法:
* Set<Map. Entry<K, V>> entrySet() 返回此映射中包含的映射关系的Set 视图。
* 实现步骤:
* 1.使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存储到一个Set集合中
* 2.遍历Set集合,获取每一个Entry对象
* 3.使用Entry对象中的方法getKey( )和getValue()获取键与值
*
*/
public class Map遍历03 {
public static void main(String[] args) {
//创建Map集合对象(多态)
Map<String,Integer> map = new HashMap<>();
map.put("路飞", 174);
map.put("索隆", 181);
map.put("山治", 180);
//1.使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存储到一个Set集合中
Set<Map.Entry<String, Integer>> set = map.entrySet();
// 2.遍历Set集合,获取每一个Entry对象
//使用迭代器遍历set集合
Iterator<Map.Entry<String, Integer>> it = set.iterator();
while(it.hasNext()){
Map.Entry<String, Integer> entry = it.next();
//3.使用Entry对象中的方法getKey( )和getValue()获取键与值
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"--->"+value);
}
System.out.println("--------------------");
//增强for遍历set
for (Map.Entry<String, Integer> entry : set ) {
String key = entry.getKey();
Integer value = entry.getValue();
System.out.println(key+"--->"+value);
}
/**
* 山治--->180
* 索隆--->181
* 路飞--->174
*/
}
}
4、HashMap存储自定义类型键值
public class Person {
private String name;
private int age;
//重写hashCode方法
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
//重写equals方法
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
//省略get/set。。。
/**
* HashMap存储自定义类型键值
* Map集合保证Key是唯一的:
* 作为key的元素,必须重写hashCode方法和equals方法,以保证key唯一
*/
public class HashMap04 {
public static void main(String[] args) {
show01();
show02();
}
/**
* HashMap存储自定义类型键值
* Key: Person类型
* Person类就必须重写hashCode方法和equals方法,为了保证key唯一
* value:String类型
* String类重写hashCode方法和equals方法,所以可以保证key唯一
*/
private static void show02() {
System.out.println("-------show02-------");
//创建HashMap集合
HashMap<Person,String> map = new HashMap<>();
//往集合中添加元素
map.put(new Person("女王",18),"英国");
map.put(new Person("秦始皇",18),"秦国");
map.put(new Person("普京",30),"俄罗斯");
map.put(new Person("女王",18),"毛里求斯");
//使用enterySet+增强for 遍历Map集合
Set<Map.Entry<Person, String>> set = map.entrySet();
for (Map.Entry<Person, String> entry : set) {
Person key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"--->"+value);
/**
* person中没有重写hashCode方法和equals方法 person中重写hashCode方法和equals方法
* Student [name=普京, age=30]--->俄罗斯 Student [name=秦始皇, age=18]--->秦国
* Student [name=女王, age=18]--->毛里求斯 Student [name=女王, age=18]--->毛里求斯
* Student [name=女王, age=18]--->英国 Student [name=普京, age=30]--->俄罗斯
* Student [name=秦始皇, age=18]--->秦国
*/
}
}
/**
* HashMap存储自定义类型键值
* Key: String类型
* String类重写hashCode方法和equals方法,所以可以保证key唯一
* value: Person类型
* value可以重复(同名同年龄的人视为同一个人)
*
*/
private static void show01() {
System.out.println("-------show01-------");
//创建HashMap集合
HashMap<String,Person> map = new HashMap<>();
//往集合中添加元素
map.put("北京",new Person("张三",18));
map.put("上海",new Person("李四",19));
map.put("广州",new Person("王五",20));
map.put("北京",new Person("赵六",18));
//使用KeySet+增强for 遍历Map集合
Set<String> set = map.keySet();
for (String key : set) {
Person value = map.get(key);
System.out.println(key+"--->"+value);
/**
* 因为String类重写hashCode方法和equals方法,所以可以保证key唯一
* 上海--->Student [name=李四, age=19]
* 广州--->Student [name=王五, age=20]
* 北京--->Student [name=赵六, age=18]
*/
}
}
}
5、LinkedHashMap 集合
/**
* java. util.LinkedHashMap<K,V> entends HashMap<K, V>
* Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。
* 底层原理:
* 哈希表+链表(记录元素的顺序)
*/
public class LinkedHashMap05 {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
map.put("a", "c");
map.put("c", "c");
map.put("b", "b");
map.put("a", "d");
System.out.println(map); //key不允许重复,无序 {a=d, b=b, c=c}
System.out.println("-----------");
LinkedHashMap<String,String> linked = new LinkedHashMap<>();
linked.put("a", "c");
linked.put("c", "c");
linked.put("b", "b");
linked.put("a", "d");
System.out.println(linked); //key不允许重复,有序 {a=d, c=c, b=b}
}
}
6、Hashtable 集合
1.底层也是哈希表,但是 Hashtable 键和值不能存储null
2.Hashtable 是最早期的双列结构,在JDK1.0版本就有了,其他的Map集合是JDK1.2之后的
/**
* java.util.Hashtable<K, V>集合 implements Map<K, V>接口
*
* Hashtable:底层也是一个哈希表, 是一个线程安全的集合,是单线程集合,速度慢
* HashMap:底层是一个哈希表, 是一个线程不安全的集合,是多线程的集合,速度快
*
* HashMap集合(之前学的所有的集合):可以存储null值, null键
* Hashtable集合,不能存储null值, nulL键
*
* Hashtable和Vector集合一样, 在jdk1.2版本之后被更先进的集合(分别是:HashMap,ArrayList)取代了
* Hashtable的子类Properties依然活跃在历史舞台
* Properties集合是一个唯一和I0流相结合的集合
*/
public class Hashtable06 {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<>();
map.put(null, "c");
map.put("b", null);
map.put(null, null);
System.out.println(map); //{null=null, b=null}
System.out.println("-----------");
Hashtable<String,String> table= new Hashtable<>();
table.put(null, "c");
table.put("b", null);
table.put(null, null);
System.out.println(table);
//java.lang.NullPointerException(空指针异常) 所以键和值都不能存储null
}
}
1、Hashtable的子类Properties依然活跃在历史舞台
2、Properties集合是一个唯一和IO流相结合的集合