hashmap統計文字列の各文字の出現回数

1032 ワード

package com.heima.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.swing.plaf.synth.SynthSpinnerUI;

/**
 * @author fenuang                 1,              2,           
 *         3,      ,                    4,         ,         
 *         5,       ,       ,        ,    ,              6,                (  )
 */
public class test1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		System.out.println("        ");
		String s = sc.nextLine();

		char[] arr = s.toCharArray();

		HashMap map = new HashMap<>();
		for (char c : arr) {
			// if(!map.containsKey(c)){
			// map.put(c, 1);
			// }else{
			// map.put(c, map.get(c)+1);
			// }
			map.put(c, !map.containsKey(c) ? 1 : (map.get(c) + 1));

		}
		// System.out.println(map);
		for (Character key : map.keySet()) {
			System.out.println(key + "=" + map.get(key));
		}
	}

}