HashMap 无序
linkedHashMap 进入顺序排序
TreeMap value排序
package com_01_testfan_02_basic; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class Day01_06_作业2 { public static void main(String[] args) { String testString="2342a2sfghgyu56asdasdaddddd"; //0 //length()-1 //char char_str =testString.charAt(testString.length()-1); //Map<String, Integer> mapcount = new LinkedHashMap<String, Integer>(); //Map<String, Integer> mapcount = new TreeMap<String, Integer>(); Map<String, Integer> mapcount = new HashMap<String, Integer>(); for (int i = 0; i < testString.length(); i++) { //System.out.println(testString.charAt(i)); String key =String.valueOf(testString.charAt(i)); //如果map 不存在 value 1 if(!mapcount.containsKey(key)) { mapcount.put(key, 1); }else { //如果存在 int value = mapcount.get(key); value +=1; mapcount.put(key, value); } } Set<String> keySet =mapcount.keySet(); for (String key : keySet) { System.out.println(key +" "+mapcount.get(key)); } //System.out.println(char_str); } }