集合框架-Map集合练习-记录字母次数思路及代码_java集合框架-Map集合练习-记录字母次数思路及代码_映射关系_02
  1 package cn.itcast.p10.map.test;
  2 
  3 import java.util.Iterator;
  4 import java.util.Map;
  5 import java.util.TreeMap;
  6 
  7 /*
  8  * 练习:
  9  * "fdgavcbsacdfs"获取该字符串中,每一个字母出现的次数。
 10  * 要求打印结果是:a(2)b(1)...;(存在映射关系有字母和次数)
 11  * 思路:
 12  * 对于结果的分析发现,字母和次数之间存在着映射关系。而且这种关系很多。
 13  * 很多就需要存储,能存储映射关系的容器有数组和Map集合
 14  * 关系一方是有序编号吗?没有!
 15  * 那就是使用Map集合。又发现可以保证唯一性的一方具备着顺序如a,b,c...
 16  * 所以可以使用TreeMap集合
 17  * 
 18  * 这个集合中最终应该存储的是字母和次数的对应关系。
 19  * 
 20  * 1,因为操作的是字符串中的字母。所以先将字符串变成字符数组。
 21  * 2,遍历字符数组,用每一个字母作为键去查Map集合这个表
 22  * 如果该字母键不存在,就将该字母作为键1作为值存储到Map集合中,
 23  * 如果该字母键存在,就将该字母键对应值取出并自增+1,再将该字母和加1后的值存储到Map集合中
 24  * 键相同值会覆盖。这样就记录了该字母的次数。
 25  * 3,遍历结束,Map集合中就记录所有字母的出现的次数。
 26  * 
 27  * 
 28  */
 29 public class MapTest {
 30 
 31     public static void main(String[] args) {
 32         // TODO Auto-generated method stub
 33         String str = "fdga-22vcbsacdfs";
 34         
 35         String s = getCharCount(str);
 36         
 37         System.out.println(s);
 38     }
 39 
 40     private static String getCharCount(String str) {
 41         // TODO Auto-generated method stub
 42         //将字符串变成字符数组
 43         char[] chs = str.toCharArray();
 44         
 45         //定义map集合表
 46         Map<Character, Integer> map = new TreeMap<Character,Integer>();
 47         
 48         for (int i = 0; i < chs.length; i++) {
 49             
 50             if (!(chs[i]>='a'&& chs[i]<='z' || chs[i]>='A'&& chs[i]<='Z')) {
 51                 continue;
 52             }
 53             //将数组中的字母作为键去查map表。
 54              Integer value = map.get(chs[i]);
 55              
 56             int count = 0;
 57              //判断值是否为null
 58             if (value!=null) {
 59                 count = value;
 60             }
 61             count++;
 62             map.put(chs[i], count);
 63              
 64         /*     int count = 1;
 65              //判断值是否为null
 66             if (value!=null) {
 67                 count = value + 1;
 68             }
 69             map.put(chs[i], count);
 70         */
 71             
 72             /*    
 73             if (value == null) {
 74                 map.put(chs[i], 1);
 75             }else {
 76                 map.put(chs[i], value+1);
 77             }
 78             */
 79         }
 80         
 81         
 82         
 83         
 84         return mapToString(map);//{a=2, b=1, c=2, d=2, f=2, g=1, s=2, v=1}
 85     }
 86 
 87     private static String mapToString(Map<Character, Integer> map) {
 88         // TODO Auto-generated method stub
 89         StringBuilder sb = new StringBuilder();
 90         
 91         Iterator<Character> it = map.keySet().iterator();
 92         
 93         while (it.hasNext()) {
 94             Character key = it.next();
 95             Integer value = map.get(key);
 96             
 97             sb.append(key+"("+value+")");
 98         }
 99         return sb.toString();
100     }
101 
102 }
MapTest