目录介绍
- 1.Map集合概述和特点
- 1.1 Map集合从何而来
- 1.2 Collection集合和Map集合的区别?
- 1.3 Map集合的特点
- 2.Map集合功能概述
- 2.1 Map集合的是一个接口
- 2.2 Map集合的功能概述
- 3.Map集合遍历案例
- 3.1 Map集合的遍历根据键找值得遍历
- 3.2 Map集合的遍历根据键值对对象的遍历
- 3.3 两种遍历原理图
- 4.HashMap集合
- 4.1 HashMap集合结构特点
- 4.2 HashMap集合键是Stirng值是String的案例
- 4.3 HashMap集合键是String值是Student的案例
- 4.4 HashMap集合键是Student值是String的案例
- 5.LinkedHashMap集合
- 5.1 LinkedHashMap的概述
- 5.2 LinkedHashMap的特点
- 6.TreeMap集合
- 6.1 TreeMap集合结构特点
- 6.2 TreeMap集合键是String值是String的案例
- 6.3 TreeMap集合键是Student值是String的案例
- 6.4 "aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
- 7.集合嵌套
- 7.1 集合嵌套之HashMap嵌套HashMap
- 7.2 集合嵌套之HashMap嵌套ArrayList
- 7.3 集合嵌套之ArrayList嵌套HashMap
- 8.HashMap和Hashtable区别
- 8.1 区别
- 9.Collections介绍
1.Map集合概述和特点
- 1.1 Map集合从何而来
- 我们通过什么东西来标识我们的学生在班级的唯一性. 我们都每一个学生应该存在一个学号 , 而这个学号是唯一的。那么我们就可以通过这个学号来表示我们学生在班级的唯一性。那么也就说,我们的学号和学生的姓名之间应该存在一个对应关系吧
- 那么我们怎么存储这样对应关系的数据呢? 针对这个情况java就给我们提供了另外一种集合进行表示,而这个集合就是Map 。Map集合结构是由两列组成,第一列被称之为键 , 第二例被称之为值 ; 并且我们都知道键应该是唯一的,而对值没有要求。
学号 姓名
stu001 张三
stu002 李四
stu003 王五
stu004 王五
-
1.2 Collection集合和Map集合的区别?
- Map集合由两列组成(双列集合) , 而Collection集合由一列组成(单列集合) ; Map集合是夫妻对 , Collection孤狼
- Collection集合中的Set集合可以保证元素的唯一性 , 而Map集合中的键是唯一的
- Collection集合的数据结构是对存储的元素是有效的,而Map集合的数据结构只和键有关系,和值没有关系
-
1.3 Map集合的特点
- 将键映射到值的对象
- 一个映射不能包含重复的键
- 每个键最多只能映射到一个值
2.Map集合功能概述
-
2.1 Map集合的是一个接口
- HashMap(底层的数据结构是哈希表)
- TreeMap(二叉树)
-
2.2 Map集合的功能概述
- Map集合的基本功能
a:添加功能
V put(K key,V value):添加元素。这个其实还有另一个功能?替换
如果键是第一次存储,就直接存储元素,返回null
如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值
b:删除功能
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并把值返回
c:判断功能
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
d:获取功能
Set<Map.Entry<K,V>> entrySet(): 返回一个键值对的Set集合
V get(Object key):根据键获取值
Set<K> keySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值的集合
e:长度功能
int size():返回集合中的键值对的对数
3.Map集合遍历案例
- 3.1 Map集合的遍历根据键找值得遍历
- 思路: 1: 获取所有的键对应的Set集合 ;2: 遍历Set获取每一个键 , 根据键找出对应的值
public static void main(String[] args) {
// 创建一个Map集合对象
Map<String , String> map = new HashMap<String , String>() ;
// 添加元素
map.put("郭靖", "黄蓉") ;
map.put("陈玄风", "梅超风") ;
map.put("杨过", "小龙女") ;
map.put("周伯通", "瑛姑") ;
// 获取所有的键对应的Set集合
Set<String> keys = map.keySet() ;
// 遍历Set获取每一个键 , 根据键找出对应的值
for(String key : keys) {
// 根据键找出对应的值
String value = map.get(key) ;
// 输出
System.out.println(key + "---" + value);
}
}
- 3.2 Map集合的遍历根据键值对对象的遍历
- 思路:
- 1: 先获取所有的键值对对象对应的Set集合 Set<Map.Entry<K,V>> entrySet() 重点(*****)
- 2: 遍历Set集合, 获取每一个元素,而每一个元素应该就是键值对对象,我们就可以调用方法获取键值对对象对应的键和值
- 思路:
public static void main(String[] args) {
// 创建对象
Map<String , String> map = new HashMap<String , String>() ;
// 添加元素
map.put("郭靖", "黄蓉") ;
map.put("陈玄风", "梅超风") ;
map.put("杨过", "小龙女") ;
map.put("周伯通", "瑛姑") ;
// 先获取所有的键值对对象对应的Set集合
// Set<Map.Entry<K,V>> entrySet() 重点(*****)
Set<Entry<String,String>> entrySet = map.entrySet() ;
// 遍历Set集合, 获取每一个元素,而每一个元素应该就是键值对对象
for(Entry<String,String> en : entrySet) {
// 获取键
String key = en.getKey() ;
// 获取值
String value = en.getValue() ;
// 输出
System.out.println(key + "---" + value);
}
}
- 3.3 两种遍历原理图
4.HashMap集合
-
4.1 集合结构特点
-
4.2 HashMap集合键是Stirng值是String的案例
public static void main(String[] args) {
/**
* 需求: 使用HashMap存储元素,键是String类型, 值也是String类型
*/
// 创建HashMap集合对象
HashMap<String , String> hm = new HashMap<String , String>() ;
// 添加元素
hm.put("张三", "23") ;
hm.put("李四", "24") ;
hm.put("王五一", "25") ;
hm.put("哈哈", "14") ;
// 遍历
// 根据键找值
// 1. 获取所有的键对应的Set集合
Set<String> keySet = hm.keySet() ;
// 2. 遍历Set集合
for(String key : keySet) {
// 然后根据键找值
String value = hm.get(key) ;
// 输出
System.out.println(key + "---" + value);
}
}
- 4.3 HashMap集合键是String值是Student的案例
public static void main(String[] args) {
/**
* 需求: 使用HashMap存储元素,键是String类型 ,值是Student类型
*/
// 创建集合对象
HashMap<String , Student> hm = new HashMap<String , Student> () ;
// 创建自定义对象
Student s1 = new Student("张三" , 23) ;
Student s2 = new Student("李四" , 24) ;
Student s3 = new Student("王五" , 25) ;
Student s4 = new Student("赵六" , 26) ;
// 把自定义对象添加到集合中
hm.put("哈哈", s1) ;
hm.put("呵呵", s2) ;
hm.put("嘿嘿", s3) ;
hm.put("嘻嘻", s4) ;
// 遍历集合
// 1: 获取所有的键值对对象对应的Set集合 entrySet
Set<Entry<String,Student>> entrySet = hm.entrySet() ;
// 2: 遍历Set集合
for(Entry<String,Student> en : entrySet) {
// 获取键
String key = en.getKey() ;
// 获取值
Student student = en.getValue() ;
// 输出
System.out.println("key: " + key + " , value: " + "[name : " + student.getName() + " , age:" + student.getAge() + "]");
}
}
- 4.4 HashMap集合键是Student值是String的案例
public static void main(String[] args) {
/**
* 需求: 使用HashMap存储元素,键是Student类型 , 值是String类型
* 如果两个对象的成员变量是相同的,那么我们认为就是同一个对象,如果是同一个对象就不能添加到HashMap集合中
*/
// 创建HashMap集合对象
HashMap<Student , String> hm = new HashMap<Student , String>() ;
// 创建自定义对象
Student s1 = new Student("张三" , 23) ;
Student s2 = new Student("李四" , 24) ;
Student s3 = new Student("王五" , 25) ;
Student s4 = new Student("赵六" , 26) ;
Student s5 = new Student("张三" , 23) ;
Student s6 = new Student("王五" , 25) ;
// 把自定义对象添加到集合中
hm.put(s1, "呵呵") ;
hm.put(s2, "嘿嘿") ;
hm.put(s3, "哈哈") ;
hm.put(s4, "嘻嘻") ;
hm.put(s5, "嘎嘎") ;
hm.put(s6, "咯咯") ;
// 遍历
// 1: 获取所有的键值对对象对应的Set集合
Set<Entry<Student,String>> entrySet = hm.entrySet() ;
// 2: 遍历集合
for(Entry<Student,String> en : entrySet) {
// 获取键
Student student = en.getKey() ;
// 获取值
String value = en.getValue() ;
// 输出
System.out.println("key: [name: " + student.getName() +" , age : " + student.getAge() + "] , value: " + value);
}
}
5.LinkedHashMap集合
-
5.1 LinkedHashMap的概述
- 底层的数据结构是链表和哈希表 , 特点: 有序
-
5.2 LinkedHashMap的案例
- Map集合的数据结构之和键有关系
public static void main(String[] args) {
// public LinkedHashMap()
LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String , String>() ;
// 添加元素
linkedHashMap.put("hello", "你好") ;
linkedHashMap.put("world", "世界") ;
linkedHashMap.put("java", "爪哇") ;
linkedHashMap.put("java", "爪哇2") ;
// 遍历
Set<String> keySet = linkedHashMap.keySet() ;
for(String key : keySet) {
String value = linkedHashMap.get(key) ;
System.out.println("key: " + key + " , value: " + value);
}
}
6.TreeMap集合
- 6.1 TreeMap集合结构特点
- TreeMap底层的数据结构是二叉树 , 可以对元素进行排序,而排序有两种方式一种是自然排序,一种是比较器排序
- 到底使用的是自然排序还是比较器排序,取决于构造方法
- Map集合的数据结构之和键有关系,和值没有关系
- 如果我们使用的是自然排序,对元素有要求,要求这个元素需要实现 Comparable 接口
public TreeMap(): 自然排序
public TreeMap(Comparator<? super K> comparator): 使用的是比较器排序
- 6.2 TreeMap集合键是String值是String的案例 * TreeMap: 键的数据结构是红黑树,可保证键的排序和唯一性
public static void main(String[] args) {
/**使用TreeMap集合存储元素,键是Integer类型 , 值是String类型*/
// 创建TreeMap集合对象
TreeMap<Integer , String> hm = new TreeMap<Integer , String> () ;
// 添加元素
hm.put(23, "乔丹") ;
hm.put(24, "科比") ;
hm.put(1, "麦迪") ;
hm.put(3, "艾弗森") ;
// 遍历
Set<Entry<Integer,String>> entrySet = hm.entrySet() ;
for(Entry<Integer,String> en : entrySet) {
// 获取键
Integer integer = en.getKey() ;
// 获取值
String value = en.getValue() ;
// 输出
System.out.println(integer + "---" + value);
}
}
- 6.3 TreeMap集合键是Student值是String的案例
public static void main(String[] args) {
/**
* 需求: 使用TreeMap集合存储元素,键是Student类型, 值是String类型
* 按照年龄大小进行排序
*/
// 创建自定义对象
Student s1 = new Student("郭靖" , 24) ;
Student s2 = new Student("杨过" , 18) ;
Student s3 = new Student("乔峰" , 25) ;
Student s4 = new Student("令狐冲" , 16) ;
// 创建TreeMap集合对象
TreeMap<Student ,String> tm = new TreeMap<Student , String>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge() ;
int num2 = (num == 0) ? s1.getName().compareTo(s2.getName()) : num ;
return num2;
}
}) ;
// 添加元素
tm.put(s1, "哈哈") ;
tm.put(s2, "呵呵") ;
tm.put(s3, "打酱油") ;
tm.put(s4, "给力") ;
// 遍历
Set<Entry<Student,String>> entrySet = tm.entrySet() ;
for(Entry<Student,String> en : entrySet) {
// 获取键
Student student = en.getKey() ;
// 获取值
String value = en.getValue() ;
// 输出
System.out.println("key: [name: " + student.getName() + " , age: " + student.getAge() + "] , value: " + value );
}
}
- 6.4 "aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)
- "aababcabcdabcde" 按照键值对的形式存储到TreeMap集合中
- 分析:
- 1,遍历字符串,获取每一个字符,然后将当前的字符作为键 , 上map集合中查找对应的值
- 2,如果返回的值不是null 对值进行+1 , 在把当前的元素作为键 , 值是+1以后的结果存储到集合中
- 3,如果返回的是是null , 不存在 , 就把当前遍历的元素作为键 , 1 作为值,添加到集合中
- 代码如下
- "aababcabcdabcde" 按照键值对的形式存储到TreeMap集合中
public static void main(String[] args) {
// 定义字符串
String s = "aababcabcdabcde" ;
// 创建TreeMap集合对象
TreeMap<Character , Integer> tm = new TreeMap<Character , Integer>() ;
// 遍历字符串
for(int x = 0 ; x < s.length() ; x++) {
// 获取当前索引出对应的字符
char ch = s.charAt(x) ;
// 找值
Integer value = tm.get(ch) ;
// 判断
if(value == null) {
tm.put(ch, 1) ;
}else {
value += 1 ;
tm.put(ch, value) ;
}
}
// 遍历Map集合按照指定的形式拼接字符串
StringBuilder sb = new StringBuilder() ;
Set<Entry<Character,Integer>> entrySet = tm.entrySet() ;
for(Entry<Character,Integer> en : entrySet) {
// 获取键
Character key = en.getKey() ;
// 获取值
Integer value = en.getValue() ;
// a(5)b(4)c(3)d(2)e(1)
// 拼接
sb.append(key).append("(").append(value).append(")") ;
}
// 把sb转换成String
String result = sb.toString() ;
// 输出
System.out.println(result);
}
7.集合嵌套
- 7.1 集合嵌套之HashMap嵌套HashMap
- 题目
传智播客
jc 基础班
陈玉楼 20
高跃 22
jy 就业班
李杰 21
曹石磊 23
- 代码
public static void main(String[] args) {
// 创建大的集合对象
HashMap<String , HashMap<String , Integer>> czbkMap = new HashMap<String , HashMap<String , Integer>>() ;
// 创建基础班的HashMap集合
HashMap<String , Integer> jcHashMap = new HashMap<String , Integer>() ;
// 添加元素
jcHashMap.put("陈玉楼", 20) ;
jcHashMap.put("高跃", 22) ;
// 把jcHashMap存储到czbkMap
czbkMap.put("jc", jcHashMap) ;
// 创建就业班的HashMap集合
HashMap<String , Integer> jyHashMap = new HashMap<String , Integer>() ;
// 添加元素
jyHashMap.put("李杰", 21) ;
jyHashMap.put("曹石磊", 23) ;
// 把jcHashMap存储到czbkMap
czbkMap.put("jy", jyHashMap) ;
System.out.println("-------------------------------------------------------------------------------");
// HashMap<String , HashMap<String , Integer>> czbkMap = new HashMap<String , HashMap<String , Integer>>() ;
Set<Entry<String,HashMap<String,Integer>>> entrySet = czbkMap.entrySet() ;
for(Entry<String,HashMap<String,Integer>> en : entrySet) {
// 获取键
String key = en.getKey() ;
System.out.println(key);
// 获取值
HashMap<String,Integer> hashMap = en.getValue() ;
// 遍历hashMap
Set<String> keySet = hashMap.keySet() ;
for(String hashMapkey : keySet){
// 根据键找值
Integer value = hashMap.get(hashMapkey) ;
// 输出
System.out.println("\t" + hashMapkey + "\t" + value);
}
// 换行
System.out.println();
}
}
- 7.2 集合嵌套之HashMap嵌套ArrayList
- 题目
三国演义
吕布
周瑜
笑傲江湖
令狐冲
林平之
神雕侠侣
郭靖
杨过
- 代码
public static void main(String[] args) {
// 创建大的集合对象
HashMap<String , ArrayList<String>> xiaoShuoMap = new HashMap<String , ArrayList<String>>() ;
// 创建三国演义的List集合
ArrayList<String> sgList = new ArrayList<String>() ;
// 添加元素
sgList.add("吕布") ;
sgList.add("周瑜") ;
// 把sgList添加到xiaoShuoMap中
xiaoShuoMap.put("三国演义", sgList) ;
// 创建笑傲江湖的List集合
ArrayList<String> xaList = new ArrayList<String>() ;
// 添加元素
xaList.add("林平之") ;
xaList.add("令狐冲") ;
// 把sgList添加到xiaoShuoMap中
xiaoShuoMap.put("笑傲江湖", xaList) ;
// 创建神雕侠侣的List集合
ArrayList<String> sdList = new ArrayList<String>() ;
// 添加元素
sdList.add("杨过") ;
sdList.add("郭靖") ;
// 把sgList添加到xiaoShuoMap中
xiaoShuoMap.put("神雕侠侣", sdList) ;
// HashMap<String , ArrayList<String>> xiaoShuoMap = new HashMap<String , ArrayList<String>>() ;
Set<Entry<String,ArrayList<String>>> entrySet = xiaoShuoMap.entrySet() ;
for(Entry<String,ArrayList<String>> en : entrySet) {
// 获取键
String key = en.getKey() ;
System.out.println(key);
// 获取值
ArrayList<String> value = en.getValue() ;
// 遍历
for(String name : value) {
System.out.println("\t" + name);
}
System.out.println();
}
}
- 7.3 集合嵌套之ArrayList嵌套HashMap
- 问题
周瑜---小乔
吕布---貂蝉
郭靖---黄蓉
杨过---小龙女
令狐冲---任盈盈
林平之---岳灵珊
- 代码
public static void main(String[] args) {
// 创建大的集合对象
ArrayList<HashMap<String , String>> al = new ArrayList<HashMap<String , String>>() ;
// 创建小的HashMap集合
HashMap<String , String> sgHashMap = new HashMap<String , String>() ;
HashMap<String , String> xaHashMap = new HashMap<String , String>() ;
HashMap<String , String> sdHashMap = new HashMap<String , String>() ;
// 添加元素
sgHashMap.put("吕布", "貂蝉") ;
sgHashMap.put("周瑜", "小乔") ;
xaHashMap.put("林平之", "岳灵珊") ;
xaHashMap.put("令狐冲", "任盈盈") ;
sdHashMap.put("郭靖", "黄蓉") ;
sdHashMap.put("杨过", "小龙女") ;
// 把小的HashMap添加到al中
al.add(sgHashMap) ;
al.add(xaHashMap) ;
al.add(sdHashMap) ;
// 遍历
// ArrayList<HashMap<String , String>> al = new ArrayList<HashMap<String , String>>() ;
for(HashMap<String , String> hm : al) {
// 根据键值对对象进行遍历
Set<Entry<String,String>> entrySet = hm.entrySet() ;
for(Entry<String,String> en : entrySet) {
// 获取键
String key = en.getKey() ;
// 获取值
String value = en.getValue() ;
// 输出
System.out.println(key + "---" + value);
}
System.out.println();
}
}
8.HashMap和Hashtable区别
- 8.1 区别
- HashMap: 特点: 允许存在null值和null键 , 线程不安全,效率高
- Hashtable: 特点: 不允许存储null值和null键 , 线程安全 ,效率低