Map
HashMap集合
01HashMapTest键或值是自定义对象
import com.itheima.公用的类.domain.Person;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*
* Map接口的实现类:HashMap
* 底层是哈希表结构, 数组+单项链表
* 无序(存储和取出方法不一样)
* 集合,可以使用null作为键和值
* 是线程不安全集合,运行速度快
*
* */
public class HashMapTest键或值是自定义对象 {
public static void main(String[] args) {
m02();
}
/*
* HashMap集合存储键值对
* 键是自定义对象,值是字符串
* 但是:Person对象,如果姓名和年龄相同,认为是同一个对象
* 那么就不要了,保证键的唯一性:
* 作为键的对象,重写方法 hashCode()和equals()
*
* 8个基本数据类型的包装类+String
* 都自动重写了hashCode()和equals()
* */
public static void m02(){
Map<Person,String>map=new HashMap<>();
map.put(new Person("张三",10),"北京市");
map.put(new Person("李四",20),"郑州市");
map.put(new Person("王五",30),"天津市");
map.put(new Person("赵六",40),"澳门行政区");
//Map集合的方法entrySet()获取Set集合,存储的是Entry对象
Set<Map.Entry<Person, String>> set = map.entrySet();
Iterator<Map.Entry<Person, String>> iterator = set.iterator();
while (iterator.hasNext()){
//取出Set集合的元素,就是Map.Entry对象
Map.Entry<Person, String> entry = iterator.next();
//取出键和值
Person key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
/*
* HashMap集合存储键值对
* 键是字符串,值是自定义对象
*/
public static void m01(){
Map<String,Person>map=new HashMap<>();
map.put("b",new Person("李四",20));
map.put("a",new Person("张三",10));
map.put("c",new Person("王五",30));
map.put("d",new Person("赵六",40));
//遍历:entrySet()方式
//Map集合方法entrySet()获取Set集合,存储对应关系对象 Map.Entry,类似于结婚证
Set<Map.Entry<String, Person>> set = map.entrySet();
Iterator<Map.Entry<String, Person>> iterator = set.iterator();
while (iterator.hasNext()){
Map.Entry<String, Person> entry = iterator.next();
String key = entry.getKey();
Person value = entry.getValue();
System.out.println(key+"="+value);
}
}
}
02用增强for遍历Map的两种方法
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/*
* Map集合,能使用增强for遍历吗
* Map是接口,不继承Iterable接口,(Iterable的子类可以直接用for增强)因此Map集合不能直接使用增强for遍历
* 但是: Map集合依赖Set遍历
* 再用for遍历Set
*/
public class 用增强for遍历Map的两种方法 {
public static void main(String[] args) {
m02();
}
/*
* entrySet()
* for增强间接遍历Map集合
* */
public static void m02(){
Map<String,String>map=new HashMap<>();
map.put("邓超","孙俪");
map.put("王宝强","马蓉");
map.put("刘德华","朱丽倩");
map.put("冯绍峰","赵丽颖");
//Map集合中的方法entrySet(),获取Set集合,存储键值对的对象的对应关系Entry
Set<Map.Entry<String, String>> set = map.entrySet();
for (Map.Entry<String, String> entry : set) {
//entry就是Set集合中的元素,Entry对象
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
/*keySet()
* for增强,间接遍历Map集合
* */
public static void m01(){
Map<String,String>map=new HashMap<>();
map.put("邓超","孙俪");
map.put("王宝强","马蓉");
map.put("刘德华","朱丽倩");
map.put("冯绍峰","赵丽颖");
//使用Map集合的方法keySet(),获取所有的键,存储到Set集合
Set<String> set = map.keySet();
//遍历Set集合,取出Set集合中的元素,正好是Map中的键
for (String key : set) {
//key就是Set集合元素,是String类型,也是Map集合中的键
String value = map.get(key);
System.out.println(key+"="+value);
}
}
}
03用迭代器遍历Map的两种方法
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*
* Map集合中的遍历,用迭代器
* 和以前的Collection集合的遍历不同
* */
public class 用迭代器遍历Map的两种方法 {
public static void main(String[] args) {
m02();
}
/*
* Map集合的遍历方式二: entrySet()
* 利用,键值对的对应关系,进行遍历的(结婚证)
* Map集合中,每一组键值对,都会具有一个对象(Entry),描述了键值对的关系
* 多个Entry对象,存储在Set集合中
* Entry接口:方法 getKey(),getValue()
*
* Map接口中定义方法:entrySet()返回Set集合,Set中存储的是键值对的关系对象Entry对象
*
* 实现步骤:
* 1:使用Map集合的方法 entrySet(),获取Set集合
* 2:遍历Set集合
* 3:取出Set集合中的元素
* Set集合中的元素,是Entry对象(一对一的映射关系对象)
* 4:使用Entry对象,获取键和值:方法 getKey(),getValue()
*
* */
public static void m02(){
Map<String,String>map=new HashMap<>();
map.put("邓超","孙俪");
map.put("王宝强","马蓉");
map.put("刘德华","朱丽倩");
map.put("冯绍峰","赵丽颖");
//1: 使用Map集合的方法 entrySet(),获取Set集合
Set<Map.Entry<String,String>> set=map.entrySet();
// 2: 遍历Set集合
Iterator<Map.Entry<String,String>> iterator=set.iterator();
while (iterator.hasNext()){
//3: 取出Set集合中的元素
Map.Entry<String, String> entry = iterator.next();//Set集合中的元素,是Entry对象 (一对一的映射关系对象)
//4: 使用Entry对象,获取键和值:entry接口定义方法 getKey() getValue()
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
/*
* Map集合遍历方式一:keySet方式
* Map的接口定义方法:
* Set keySet() Map把集合中所有的键,拿出来,存储到Set集合中
*
* 实现步骤:
* 1:Map集合方法keySet(),键存储在Set集合
* 2:遍历Set集合
* 3:取出Set集合的元素
* Set集合中的元素,就是Map集合中的键
* 4:Map集合的方法get()传递键,获取值
*
* */
public static void m01(){
Map<String,String>map=new HashMap<>();
map.put("邓超","孙俪");
map.put("王宝强","马蓉");
map.put("刘德华","朱丽倩");
map.put("冯绍峰","赵丽颖");
//1:Map集合方法keySet(),键存储在Set集合
Set<String> set = map.keySet();
//2:遍历Set集合
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()){
//3: 取出Set集合的元素
String key = iterator.next();// Set集合中的元素,就是Map集合中的键
//4: Map集合的方法get()传递键,获取值
String value = map.get(key);
System.out.println(key+"="+value);
}
}
}
LinkedHashMap
/*
* java.util.LinkedHashMap类,继承HashMap,实现Map接口
* 底层数据结构是哈希表,双向链表
* LinkedHashMap是有序的,不重复的集合
* 线程不安全的
*/
import java.util.LinkedHashMap;
import java.util.Map;
public class LinkedHashMapTest {
public static void main(String[] args) {
Map<Integer,String>map=new LinkedHashMap<>();
map.put(1,"a");
map.put(2,"b");
map.put(3,"c");
map.put(4,"d");
//map.put(new Integer(3),"c");重复了
System.out.println(map);
}
}
Properties集合
import java.util.Properties;
import java.util.Set;
/*
* java.util.Properties类,是Map接口的实现类
* Properties类继承Hashtable类,Hashtable实现Map接口
*
* Hashtable类:底层数据结构是哈希表,单项链表
* 集合,不能存储null,作为键或者值都不行
* 是线程安全的集合,运行速度慢
*
* 现在不怎么用Hashtable类了,已经被先进的HashMap取代
* 但是HashTable的子类Properties依然活跃在开发舞台
*
* Properties集合自身特点
* 底层是哈希表,无序不重复的集合,线程安全的集合
* 能和IO流对象结合使用,实现数据的持久化
*
* Properties集合泛型无法指定,Sun公司将泛型锁定为String类型
*
*
* */
public class PropertiesTest {
public static void main(String[] args) {
m03();
}
/*
* Properties类特有方法
* 将集合中的所有键,存储到Set集合
* Map接口使用方法: keySet()
* Properties类使用方法: stringPropertyNames()
*/
public static void m03(){
Properties properties = new Properties();
properties.setProperty("a","1");
properties.setProperty("b","2");
//相当于给b重新赋值,b不能重复
properties.setProperty("b","3");
properties.setProperty("c","3");
//获取所有键,存储到Set集合
Set<String> set = properties.stringPropertyNames();
for (String key : set) {
//获取集合中的值,getProperty()
System.out.println(key+"="+properties.getProperty(key));
}
}
/*
* Properties类特有方法
* 依据键值对的方法:
* Map接口使用方法是get
* Properties获取值,getProperty(String key)返回字符串类型的值
* */
public static void m02(){
Properties properties = new Properties();
properties.setProperty("a","1");
properties.setProperty("b","2");
properties.setProperty("c","3");
properties.setProperty("d","4");
//获取c键
String value = properties.getProperty("c");
System.out.println(value);
}
/*
* Properties类特有方法
* 存储键值对方法:
* Map接口存储键值对,put方法
* Properties存储键值对,setProperty(String key,String value)
*
* */
public static void m01(){
Properties properties = new Properties();
properties.setProperty("a","1");
properties.setProperty("b","2");
properties.setProperty("c","3");
System.out.println(properties);
}
}
Map
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/*
* Map接口:此接口下的所有集合,都是键值对集合
* 必须保证键的唯一性,一个键只能对应一个值
* 一对一的映射关系(一夫一妻制)
*
* 使用Map接口实现类 HashMap 无序的
* */
public class MapTest {
public static void main(String[] args) {
m05();
}
/*
* Map接口中的方法
* Collection values() 将Map就集合中的所有的值,存储到另一个集合中
* 返回值是Collection集合
*/
public static void m05(){
//创建Map集合,String作为键,String作为值
Map<String,String>map=new HashMap<>();
map.put("邓超","孙俪");
map.put("刘德华","朱丽倩");
map.put("王宝强","马蓉");
System.out.println(map);
//集合中的全部值,存储到Collection集合
Collection<String> collection = map.values();
//遍历获取元素
for (String s : collection) {
System.out.println(s);
}
}
/*
* Map接口中的方法
* 移除集合中的键值对
* V remove(Key) 传递键,删除此键值对
* 并返回被删除之前的值
*/
public static void m04(){
//创建Map集合,String作为键,String作为值
Map<String,String>map=new HashMap<>();
map.put("邓超","孙俪");
map.put("刘德华","朱丽倩");
map.put("王宝强","马蓉");
System.out.println(map);
//移除刘德华
String value = map.remove("刘德华");
System.out.println(map);
System.out.println(value);
}
/*
* Map接口中的方法
* boolean containsKey(Key) 判断集合中是否包含此键
* boolean containsValue(Value) 判断集合中是否包含此值
* 如果包含,返回true
* */
public static void m03(){
//创建Map集合,String作为键,String作为值
Map<String,String>map=new HashMap<>();
map.put("邓超","孙俪");
map.put("刘德华","朱丽倩");
map.put("王宝强","马蓉");
System.out.println(map);
//判断集合中是否包含邓超键
boolean b = map.containsKey("邓超");
System.out.println(b);
//判断集合中是否包含孙俪值
b= map.containsValue("孙俪");
System.out.println(b);
}
/*
* Map接口中的方法
* 取出集合中的值
* V get(key)
* 参数传递键对象,返回这个键对应的值
*
* 集合中如果没有此键,则返回null
*
* 常识:获取元素:元素是基本类型,数字,如果没有则返回负数
* 获取元素:元素是引用类型,如果没有则返回null
* */
public static void m02(){
//创建Map集合,String作为键,String作为值
Map<String,String>map=new HashMap<>();
map.put("邓超","孙俪");
map.put("刘德华","朱丽倩");
map.put("王宝强","马蓉");
System.out.println(map);
//根据键,获取值
String value = map.get("王宝强");
System.out.println(value);
}
/*
* Map接口中的方法
* 向集合中存储键值对
* V put(Key,Value)
* put方法带有返回值的方法,返回值的类型
*
* 返回值一般情况下返回null
* 当向集合中存储了重复的键,值会被覆盖(相当于重新赋值)
* 返回被覆盖之前的值
* */
public static void m01(){
//创建Map集合,存储键值对,String作为键,Interger作为值
Map<String,Integer>map=new HashMap<>();
//集合方法put存储键值对
map.put("aaa",123);
map.put("bbb",456);
map.put("ccc",789);
System.out.println(map);
//类似于重新给“bbb”对应的键465重新赋值成147
//可以对键和值重新赋值,但不管怎样,他们都是唯一的
Integer i = map.put("bbb", 147);
//返回被覆盖之前的值,这个i就是覆盖之前的值
System.out.println(i);
System.out.println(map);
}
}
Map集合练习题字符串出现的次数
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*
* 已知字符串,计算字符串中每个字符出现的次数
*/
public class Map集合练习题字符串出现的次数 {
public static void main(String[] args) {
//定义字符串
String str="aasffds张张66564gdfdds";
//字符串转成字节数组
char[] chars = str.toCharArray();
//定义集合,键是字符,值是整数
Map<Character,Integer>map=new HashMap<>();
//遍历数组,取出数组中的每个元素
for (char ch : chars) {
//ch表示数组中的每个元素,作为键,到集合中找值(Integer类型)
Integer value = map.get(ch);
//判断值是不是null,第一个存入的
if(value==null){
//字符没有出现过,向集合中存储1,字符是键
map.put(ch,1);
}else {
//value值不是空,值++再次存储到集合
value++;
map.put(ch,value);
}
}
//遍历Map集合
// Set<Character> set = map.keySet();
for (Character key : map.keySet()) {
Integer value = map.get(key);
System.out.println("字符"+key+" "+"出现了"+value+"次");
}
}
}
-------------------------------------------------------
可变参数
/*
* JDK1.5版本出现的新特点:
* 方法的可变参数
*
* 通过刚才的需求,分析出一个特点:
* 方法中的参数,参数类型是固定的,但是参数的个数不确定
*
* 方法的可变参数:个数可变,类型不能变
* 语法:
* 数据类型...变量
* 可变参数的本质是一个数组
* */
public class VarArgumentsTest {
public static void main(String[] args) {
/*
* 调用了一个可变参数的方法
* 参数传递是而任意的
* */
int sum = getSum(1, 2, 3);
System.out.println(sum);
m(1,2,0,1,2,3,4,5);//1属于a,2属于b,剩下的是c
}
/*
* 注意事项:
* 1:一个方法的参数,只能有一个是可变
* 2:可变参数只能写在参数列表的最后一个
* */
public static void m(int a,int b,int...c){
}
public static int getSum(int...a){
int sum=0;
//遍历数组,取出火元素,再求和
for (int i : a) {
sum+=i;
}
return sum;
}
//上面方法的第二种遍历,用普通遍历方式,a是数组的变量名,类似于arr
public static int getSum1(int...a){
int sum1=0;
for (int i = 0; i < a.length; i++) {
sum1=+a[i];
}
return sum1;
}
}
-------------------------------------------------------
斗地主案例
/*
*核心思想:每一个牌,定义自己的编号 0-53
*
* 斗地主 小到大: 333344445555....AAAA2222(字符串)
* 0123456789..........50 51(编号)
*/
import java.util.*;
public class Poker {
public static void main(String[] args) {
//定义Map集合,键存储的是自定义编号,值就是字符串
Map<Integer,String>mapPoker=new HashMap<>();
//定义List集合,存储编号
List<Integer>numberList=new ArrayList<>();
//定义数组,存储花色
String[]colors={"♠","♥","♣","♦"};
//定义数组,存储13个点数
//对存的数进行排序,3是最小的,编号是0,数组中的元素,从3开始写
String[]numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
//自定义的编号0-53
int num=0;
//遍历数组,向Map集合中,存储键值对,键是编号,值是字符串
//外循环应该是点数
for (String number : numbers) {
//内循环是花色
for (String color : colors) {
//拼接字符串,存储到Map集合
mapPoker.put(num,color+number);
//存储List集合,只存储编号
numberList.add(num);
num++;
}
}
//单独存储王牌
mapPoker.put(52,"小王");
mapPoker.put(53,"大王");
//在编号集合中存储大小王的编号
numberList.add(52);
numberList.add(53);
//洗牌,洗的是每个牌的编号
Collections.shuffle(numberList);
//发牌:定义4个玩家集合,表示玩家和底牌,发的是编号
List<Integer>p1=new ArrayList<>();
List<Integer>p2=new ArrayList<>();
List<Integer>p3=new ArrayList<>();
List<Integer>bottom=new ArrayList<>();
//遍历,对存储的编号的集合进行,发给玩家,是打乱的数序
for (int i = 0; i < numberList.size(); i++) {
//判断,如果索引小于3,预留三张底牌
if(i<3){
bottom.add(numberList.get(i));
}else if (i%3==0){
p1.add(numberList.get(i));
}else if(i%3==1){
p2.add(numberList.get(i));
}else if(i%3==2){
p3.add(numberList.get(i));
}
}
//循环发牌后,对玩家集合中的编号进行排序
Collections.sort(p1);
Collections.sort(p2);
Collections.sort(p3);
//看牌,遍历玩家集合,集合中的编号,作为键,到Map集合中获取值(字符串)
look(p1,mapPoker);
look(p2,mapPoker);
look(p3,mapPoker);
look(bottom,mapPoker);
}
/*
* 方法实现看牌
* 传递玩家集合,传递Map集合
* 通过玩家的排好顺序的键,去找Map集合中对应的字符串值
* */
public static void look(List<Integer>player,Map<Integer,String>map){
//遍历List集合
for (Integer key : player) {
//key是集合元素,是牌的编号,他作为键,到Map集合中获取值(字符串)
String value = map.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}
-------------------------------------------------------
集合嵌套
List嵌套List集合
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
* 集合的嵌套: 一个List集合,存储的元素是另一个List集合
* 另一个List集合,存储的元素是字符串
*
* 外面的集合叫做大集合,里面的集合叫做小集合
* List.add ( List )
*
* 需求:
* 创建3个List集合,每个集合存储一些字符串
* 3个List集合,存储到另一个List集合,遍历
*/
public class List嵌套List集合 {
public static void main(String[] args) {
//创建3个List集合
List<String>small1=new ArrayList<>();
List<String>small2=new ArrayList<>();
List<String>small3=new ArrayList<>();
//存储字符串
small1.add("aaa");
small1.add("111");
small2.add("bbb");
small2.add("222");
small3.add("smaccc");
small3.add("333");
//创建大List,存储小List集合类型
List<List<String>>bigList=new ArrayList<>();
//存储元素,大集合中,将2个小List集合当做元素进行存储
bigList.add(small1);
bigList.add(small2);
bigList.add(small3);
//遍历大集合
Iterator<List<String>> bigIt = bigList.iterator();
while (bigIt.hasNext()){
//取出的元素,是小集合 List
List<String> smallList = bigIt.next();
//再对小集合进行遍历
Iterator<String> smallIt = smallList.iterator();
while (smallIt.hasNext()){
//取出小集合的元素,是字符串
String next = smallIt.next();
System.out.println(next);
}
}
}
}
List嵌套Map
import java.util.*;
/*
* List集合嵌套Map集合
* List集合中,存储的元素是Map集合
*
* 1班有第三名同学,学号和姓名分别为:001=张三,002=李四,003=王五,
* 2班有三名同学,学号和姓名分别为:001=黄晓明,002=杨颖,003=刘德华,004=朱丽倩,
* 请将同学的信息以键值对的形式存储到2个Map集合中,在将2个Map集合存储到List集合中。
*/
public class List嵌套Map {
public static void main(String[] args) {
//创建2个Map集合,分别存储2个班级的学生信息
Map<String,String>map1=new HashMap<>();
Map<String,String>map2=new HashMap<>();
//Map集合存储学生信息
map1.put("001","张三");
map1.put("002","李四");
map1.put("003","王五");
map2.put("001","黄晓明");
map2.put("002","杨颖");
map2.put("003","刘德华");
map2.put("004","朱丽倩");
//创建List集合,存储个Map集合类型
List<Map<String,String>>bigList=new ArrayList<>();
//存储元素,将2个Map集合当做元素存储到List集合中
bigList.add(map1);
bigList.add(map2);
//遍历外边的大集合List
Iterator<Map<String, String>> bigIt = bigList.iterator();
while (bigIt.hasNext()){
//取出List集合中的元素,这元素是Map集合
Map<String, String> map = bigIt.next();
//继续对Map进行遍历,用Map集合的方法,获取所有键,存储到Set集合中
Set<String> set = map.keySet();
//用迭代器,遍历Set集合
Iterator<String> smallIt = set.iterator();
while (smallIt.hasNext()){
//取出Set集合中的元素,就是Map集合中的键
String key = smallIt.next();
//Map集合的方法get()获取值
String value = map.get(key);
System.out.println(key+"="+value);
}
}
}
Map嵌套Map
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/*
* 集合的嵌套,Map嵌套Map
* Map集合中,存储的元素还是Map
* 需求:有以下数据结构,使用集合存储。
* - java基础班 集合 存储的是 学号 键,值学生姓名
* - 001 张三
* - 002 李四
* - java就业班
* - 001 王五
* - 002 赵柳
* 小集合:存储班级学生信息的集合 键是学号,值是姓名
* 2个Map小集合,对基础班和就业班进行分别存储
* 大集合:存储的键是班级名字(基础班,就业班)
* 存储的值是2个小集合
*/
public class Map嵌套Map {
public static void main(String[] args) {
//创建小Map集合,存储的键值对,是学号和姓名
Map<String,String>javaSeMap=new HashMap<>();
Map<String,String>javaEeMap=new HashMap<>();
//分别对基础班和就业班的数据进行存储
javaSeMap.put("001","张三");
javaSeMap.put("002","李四");
javaEeMap.put("001","王五");
javaEeMap.put("002","赵六");
//创建大Map集合,存储键班级名,存储值2个小集合
Map<String,Map<String,String>>heimaMap=new HashMap<>();
//对大集合进行存储数据
heimaMap.put("基础班",javaSeMap);
heimaMap.put("就业班",javaEeMap);
//用方法进行遍历
// keySetMap(heimaMap);
entrySetMap(heimaMap);
}
/*
* 用entrySet()
* 方法实现Map的嵌套遍历
* */
public static void entrySetMap(Map<String,Map<String,String>>heimaMap){
//使用黑马大集合,的方法entrySet(),获取大集合中的键值对对应关系对象,Entry对象,存储到Set集合中
Set<Map.Entry<String, Map<String, String>>> heimaSet = heimaMap.entrySet();
//迭代器遍历Set集合
Iterator<Map.Entry<String, Map<String, String>>> heimaIt = heimaSet.iterator();
while (heimaIt.hasNext()){
//取出Set集合的元素,是黑马集合的键值对Entry对象
Map.Entry<String, Map<String, String>> heimaEntry = heimaIt.next();
//键值对关系对象的方法getKey() getValue()取出黑马集合的键(班级名字)和值(小Map集合)
String className = heimaEntry.getKey();
Map<String, String> classMap = heimaEntry.getValue();
//使用小集合的方法entrySet(),获取Set集合
Set<Map.Entry<String, String>> classSet = classMap.entrySet();
//迭代器遍历Set集合
Iterator<Map.Entry<String, String>> classSetIt = classSet.iterator();
while (classSetIt.hasNext()){
//取出Set集合中的元素,是小Map集合的键值对关系Entry对象,
Map.Entry<String, String> classEntry = classSetIt.next();
//键值对关系对象的方法getKey() getValue(),取出小Map的键(学号)和值(姓名)
String number = classEntry.getKey();
String name = classEntry.getValue();
System.out.println(className+":"+number+"="+name);
}
}
}
/*用keySet()
* 方法实现Map的嵌套遍历
* */
public static void keySetMap(Map<String,Map<String,String>>heimaMap){
//使用黑马Map集合,大集合,方法keySet()获取大集合的键(班级名字)
Set<String> heimaKeySet = heimaMap.keySet();
//迭代器遍历Set集合
Iterator<String> heimaKeyIt = heimaKeySet.iterator();
while (heimaKeyIt.hasNext()){
//取出Set集合的元素,取出的是大集合的键(班级名字:基础班 就业班)
String classNameKey = heimaKeyIt.next();
//获取黑马Map集合的值(小Map集合),传递键进行获取
Map<String, String> classMap = heimaMap.get(classNameKey);
//再用classMap.keySet()取出小Map班级集合的键
Set<String> classKeySet = classMap.keySet();
//迭代器遍历Set集合
Iterator<String> classKeyIt = classKeySet.iterator();
while (classKeyIt.hasNext()){
//取出Set集合的元素,是小Map班级集合中的键(学号)
String number = classKeyIt.next();
//再取出小Map班级集合中的值(学生姓名)
String name = classMap.get(number);
//输出 班级:学号+姓名
System.out.println(classNameKey+":"+number+"="+name);
}
}
}
}