map集合没有iterator方法,所以不能直接迭代
直接看下面的代码和第一行的注解(加了注释之后变黑看不清了,所以前面没加注释)
1、map的第一种遍历:遍历map的所有值:method1()
获取所有的键的集合:Set<K> keySet() ,根据键获取值map.get(key)
然后for循环或迭代器遍历
2、map的第二种遍历:将键值对看成一个对象,双列集合变成单列集合:method2()
Set<Map.Entry<K,V>> entrySet()
Entry是Map的内部接口,将键和值封装成了Entry对象,并存储在Set集合中
3、Entry有两种写法:
Map.Entry<String, Integer> entry = it.next();父类引用指向子类对象
Entry<String, Integer> entry = it.next();直接就是个Entry对象
package cn.xinhua;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class SetTest {
public static void main(String[] args) {
HashMap<String, Integer> hap = new HashMap<>();
hap.put("吴彦祖", 23);
hap.put("周杰伦", 24);
hap.put("林俊杰", 25);
hap.put("陈奕迅", 26);
method1(hap);
method2(hap);
}
public static void method2(HashMap<String, Integer> hap) {
// 增强for循环
Set<Map.Entry<String, Integer>> en = hap.entrySet();
/*
* for (Entry entry : en) { System.out.println(entry.getKey() +
* " = " + entry.getValue()); }
*/
// 迭代器
Iterator<Map.Entry<String, Integer>> it = en.iterator();// 就像之前做的那个班级里面嵌套Person
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
public static void method1(HashMap<String, Integer> hap) {
Set<String> set = hap.keySet();
// 增强for循环遍历
/*
* for (String string : set) { System.out.println(hap.get(string)); }
*/
// 迭代器遍历
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String key = it.next();
System.out.println(key + " = " + hap.get(key));
}
}
}
思路:
代码实现如下:
package cn.xinhua;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class TreeSetTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串,我们将统计字符出现的次数:");
String str = sc.nextLine();
char[] cha = str.toCharArray();
HashMap<Character, Integer> ham = new HashMap<>();
for (int i = 0; i < cha.length; i++) {
ham.put(cha[i], (!ham.containsKey(cha[i]) ? 1 : ham.get(cha[i]) + 1));
}
// hashmap遍历
//普通遍历
/*for(Character key : ham.keySet()) {
System.out.println(key + ham.get(key));
}*/
//Entry遍历
Set<Map.Entry<Character, Integer>> en = ham.entrySet();
Iterator<Map.Entry<Character, Integer>> it = en.iterator();
while (it.hasNext()) {
Map.Entry<Character, Integer> men = it.next();
System.out.println(men.getKey() + "有" + men.getValue() + "个");
}
}
}
这个挺简单的,就跟之前的ArrayList嵌套ArrayList一样:
package cn.xinhua;
import cn.njupt.Student;
import java.util.HashMap;
public class HashmapInHashmap {
public static void main(String[] args) {
HashMap<HashMap<Student, String>, Integer> bigH = new HashMap<>();
HashMap<Student, String> smaH1 = new HashMap<>();
smaH1.put(new Student("吴彦祖", 23), "新疆");
smaH1.put(new Student("周杰伦", 24), "广东");
smaH1.put(new Student("林俊杰", 25), "南京");
smaH1.put(new Student("陈奕迅", 26), "兴化");
HashMap<Student, String> smaH2 = new HashMap<>();
smaH2.put(new Student("范冰冰", 32), "和田");
smaH2.put(new Student("楊冪", 34), "江苏");
smaH2.put(new Student("周冬雨", 35), "云南");
smaH2.put(new Student("李冰冰", 36), "甘肃");
bigH.put(smaH1, 88);
bigH.put(smaH2, 99);
for (HashMap<Student, String> has : bigH.keySet()) {
System.out.println(has + " = " + bigH.get(has));
for (Student student : has.keySet()) {
System.out.println(student + " = " + has.get(student));
}
System.out.println("-------------------------------------------");
}
}
}
输出:
{Student [name=陈奕迅, age=26]=兴化, Student [name=吴彦祖, age=23]=新疆, Student [name=林俊杰, age=25]=南京, Student [name=周杰伦, age=24]=广东} = 88
Student [name=陈奕迅, age=26] = 兴化
Student [name=吴彦祖, age=23] = 新疆
Student [name=林俊杰, age=25] = 南京
Student [name=周杰伦, age=24] = 广东
-------------------------------------------
{Student [name=范冰冰, age=32]=和田, Student [name=楊冪, age=34]=江苏, Student [name=周冬雨, age=35]=云南, Student [name=李冰冰, age=36]=甘肃} = 99
Student [name=范冰冰, age=32] = 和田
Student [name=楊冪, age=34] = 江苏
Student [name=周冬雨, age=35] = 云南
Student [name=李冰冰, age=36] = 甘肃
-------------------------------------------