★★★★Map集合的4種遍歷方式以及Map集合的嵌套的遍歷★★★★
public class traverseMap {
public static void main(String[] args) {
HashMap<Integer,String> has = new HashMap<>();
has.put(1,"張三");
has.put(2,"李四");
has.put(3,"王五");
show1(has);
show2(has);
}
/*
使用keySet()對Map集合進行遍歷 Set<K> keySet() K-->鍵的數據類型
將集合的鍵存放到Set集合中,再對鍵進行遍歷
通過Map集合中的鍵找值的方法get(key)獲取到值
遍歷方式分爲foreach循環和while循環
|--foreach循環也就是對通過keySet()方法獲得的鍵Set集合進行遍歷
|--while循環則是通過創建一個Iterator迭代器對象進行遍歷,再通過next()去獲取每個key,隨之獲取值.
總結:反正keySet()就是通過鍵找值的方法去遍歷
*/
private static void show2(HashMap<Integer, String> has) {
//通過foreach循環遍歷
System.out.println("=========通過keySet()方法用foreach循環遍歷");
Set<Integer> keys = has.keySet();
for (Integer key : keys) {
String value = has.get(key);//通過鍵getValue()
System.out.println(key + " = " + value);
}
System.out.println("=========通過keySet()方法用迭代器循環遍歷");
Set<Integer> keysIter = has.keySet();
Iterator<Integer> it = keysIter.iterator();
while(it.hasNext())
{
Integer key = it.next(); //通過next()取到每次遍歷的key
String value = has.get(key); //通 過key用get()方法獲取到value
System.out.println(key + " = " + value);
}
}
/*
通過entrySet()方法對Map進行遍歷,將每個鍵值對封裝爲對象,再通過對象去getValue()和getKey()
Set<Map.Entry<K,v>> K-->鍵的數據類型 V-->值的數據類型
遍歷方式分爲foreach和while
|--foreach循環先需要通過entrySet()方法把所有的鍵值對封裝爲對象
再通過此循環對每個entry對象進行getKey()和getValue()
|--while循環同上也是先通過entrySet()方法把所有的鍵值對封裝爲對象
再通過獲取到的entry對象創建一個Iterator對象(迭代的泛型和上面的Set<...>裏面的泛型一致),
通過while循環進行遍歷,通過迭代器中的next()方法獲取每個對象,再通過獲取到的對象getKey()和getKey()
總結:更加的貼合面向對象
*/
private static void show1(HashMap<Integer, String> has) {
Set<Map.Entry<Integer, String>> entries = has.entrySet(); //將所有的鍵值對封裝爲對象
//通過foreach循環 ,前面的遍歷的類型是和上面Set集合中的泛型是一樣的
System.out.println("==========通過entrySet()方法foreach循環進行遍歷");
for (Map.Entry<Integer, String> entry : entries) {
Integer key = entry.getKey(); //獲取到鍵
String value = entry.getValue(); //獲取到值
System.out.println(key + " = " + value);
}
//通過while循環,需要創建迭代器,通過迭代器對entries對象進行遍歷
System.out.println("=========通過entrySet方法while循環進行遍歷");
Set<Map.Entry<Integer,String>> entries1 = has.entrySet();
Iterator<Map.Entry<Integer,String>> it = entries1.iterator();
while(it.hasNext())
{
Map.Entry<Integer, String> entry = it.next(); //獲取到每次遍歷的對象
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key + " = " + value);
}
}
}
Map集合中嵌套Map集合
/*
集合的嵌套
Map集合中嵌套Map集合
但是在Map集合中嵌套Map集合的意義不大,不貼合面向對象
*/
public class MapNest01 {
public static void main(String[] args) {
HashMap<String,HashMap<String,Integer>> has = new HashMap<>();
//創建就業班級的集合
HashMap<String,Integer> hasJob = new HashMap<>();
hasJob.put("張三",85);
hasJob.put("李四",75);
hasJob.put("王五",95);
//創建基礎班的集合
HashMap<String,Integer> hasBas = new HashMap<>();
hasBas.put("張三",85);
hasBas.put("李四",75);
hasBas.put("王五",95);
has.put("就業班",hasJob);
has.put("基礎班",hasBas);
System.out.println("========通過keySet()對集合進行遍歷");
show01(has);
System.out.println("========通過entrySet()對集合進行遍歷");
show02(has);
}
//對集合進行遍歷使用keySet()方法遍歷嵌套的集合
private static void show02(HashMap<String, HashMap<String, Integer>> has) {
Set<String> keys = has.keySet();
for (String key : keys) {
System.out.print(key + " : ");
//通過遍歷的key取到裏面的值,而裏面的值就是此key可以所對應的集合(也就是"就業班"對應的hasJob集合...)
HashMap<String,Integer> hasVal = has.get(key);
//取到的集合也需要進行遍歷 ,就採用最簡單的foreach循環
for (String keyVal : hasVal.keySet()) {
Integer valVal = hasVal.get(keyVal); //通過獲取到的集合中的鍵再獲取到值
System.out.println(keyVal + " = " + valVal);
}
}
}
/*
通過entrySet()集合將裏面的鍵值對封裝爲對象,再通過對象getKey()和getValue()
而getValue()獲取到的值就是has集合裏面嵌套的一個HashMap集合
在對此集合進行遍歷(繼續可以用foreach循環的方法或者迭代器都可以)
*/
private static void show01(HashMap<String, HashMap<String, Integer>> has) {
//將has集合中的鍵值對通過entrySet()封裝爲對象
Set<Map.Entry<String , HashMap<String , Integer>>> entries = has.entrySet();
//通過增強for循環對其進行遍歷,獲取到裏面的鍵,再通過鍵獲取到裏面的值,
//而裏面的值就是嵌套的集合,所以需要對嵌套的集合繼續進行遍歷
for (Map.Entry<String, HashMap<String, Integer>> entry : entries) {
String key = entry.getKey();
HashMap<String, Integer> hasValue = entry.getValue(); //此value是一個嵌套的集合,需要繼續遍歷
for (String keyVal : hasValue.keySet()) {
Integer value = hasValue.get(keyVal);
System.out.println(keyVal + " = " + value);
}
}
}
}
Map集合中嵌套List集合(更加的貼合面向對象)
public class MapNest02 {
public static void main(String[] args) {
HashMap<String,ArrayList<Person>> hashMap = new HashMap<>();
ArrayList<Person> arrJob = new ArrayList<>();
Person p1 = new Person("張三",18,89);
Person p2 = new Person("李四",22,78);
arrJob.add(p1);
arrJob.add(p2);
ArrayList<Person> arrBas = new ArrayList<>();
Person p3 = new Person("王五",33,56);
Person p4 = new Person("趙六",35,66);
arrBas.add(p3);
arrBas.add(p4);
hashMap.put("就業班",arrBas);
hashMap.put("基礎班",arrJob);
show02(hashMap);
show01(hashMap);
}
/*
通過keySet()方法將key
*/
private static void show02(HashMap<String, ArrayList<Person>> hashMap) {
System.out.println("===========通過keySet()方法");
Set<String> keys = hashMap.keySet();
for (String key : keys) {
System.out.println(key + " : ");
ArrayList<Person> people = hashMap.get(key); //通過遍歷到的hashMap集合的鍵獲取到值
for (Person person : people) {
System.out.println("姓名: " + person.getName() + " 年齡: " + person.getAge() + " 分數:" + person.getSecore());
}
}
}
//通過entrySet()方法
private static void show01(HashMap<String, ArrayList<Person>> hashMap) {
System.out.println("===========通過entrySet()方法");
Set<Map.Entry<String, ArrayList<Person>>> entries = hashMap.entrySet();
//對此對象進行遍歷
for (Map.Entry<String, ArrayList<Person>> entry : entries) {
String key = entry.getKey();
System.out.println(key + " : ");
ArrayList<Person> value = entry.getValue(); //這個獲取到的就是一個對象
//對裏面的嵌套集合繼續進行遍歷
for (Person person : value) {
System.out.println("姓名 : " + person.getName() + " , 年齡: " + person.getAge() + " ,分數:" + person.getSecore());
}
}
}
}
//Person類
public class Person {
private String name;
private int age;
private int secore;
public Person() {
}
public Person(String name, int age, int secore) {
this.name = name;
this.age = age;
this.secore = secore;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSecore() {
return secore;
}
public void setSecore(int secore) {
this.secore = secore;
}
}