1. 程式人生 > >javaSE (二十六)map集合遍歷(兩種方法)、輸入字元,計算字元出現次數(用map實現)、HashMap巢狀HashMap

javaSE (二十六)map集合遍歷(兩種方法)、輸入字元,計算字元出現次數(用map實現)、HashMap巢狀HashMap

1、map集合遍歷:
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<String, Integer> 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)); } } }

2、輸入字元,計算字元出現次數(用map實現):

思路:

  1. 輸入:字串變成字元陣列,用迴圈輸入到HashMap中,
  2. 判斷,如果hashmap中沒有key是陣列中的元素,這個key的V alue=1,如果有,則 + 1;
  3. 遍歷:第一種遍歷直接拿的是ket的集合,第二種是用Entry,兩個都可以

程式碼實現如下:

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() + "個");
		}
		
		

	}

}

3、HashMap巢狀HashMap:

這個挺簡單的,就跟之前的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] = 甘肅
-------------------------------------------