黑马程序员技术交流社区

标题: TreeMap字符串中字符出现次数练习 [打印本页]

作者: itheima_llt    时间: 2015-4-19 11:57
标题: TreeMap字符串中字符出现次数练习
本帖最后由 itheima_llt 于 2015-4-19 11:58 编辑
  1. /*
  2. 练习:
  3. "sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。

  4. 希望打印结果:a(1)c(2).....
  5. 通过结果发现,每一个字母都有对应的次数。
  6. 说明字母和次数之间都有映射关系。

  7. 注意了,当发现有映射关系时,可以选择map集合。
  8. 因为map集合中存放就是映射关系。


  9. 什么使用map集合呢?
  10. 当数据之间存在这映射关系时,就要先想map集合。

  11. 思路:
  12. 1,将字符串转换成字符数组。因为要对每一个字母进行操作。

  13. 2,定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合。

  14. 3,遍历字符数组。
  15.         将每一个字母作为键去查map集合。
  16.         如果返回null,将该字母和1存入到map集合中。
  17.         如果返回不是null,说明该字母在map集合已经存在并有对应次数。
  18.         那么就获取该次数并进行自增。,然后将该字母和自增后的次数存入到map集合中。覆盖调用原键所对应的值。

  19. 4,将map集合中的数据变成指定的字符串形式返回。

  20. */
  21. import java.util.*;
  22. public class TreeMapStringTest
  23. {
  24.         public static void sop(Object obj)
  25.         {
  26.                 System.out.println(obj);
  27.         }

  28.         public static void main(String[] args)
  29.         {
  30.                 //1 创建一个字符串对象
  31.                 String str = new String("akabAf3+22*-cdCkaAbcdefa");
  32.                 //打印目标字符串
  33.                 sop("被访问的字符串:"+str);

  34.                 //2 获取字符串中每个字母出现的次数,以指定字符串的形式返回
  35.                 sop("每个字母出现的次数:"+getCharCount(str));

  36.         }

  37.         //定义方法获取字符串中每个字母出现的次数,以指定字符串的形式返回
  38.         public static String getCharCount(String str)
  39.         {
  40.                 //1 把字符串变成字符数组
  41.                 char[] chs = str.toCharArray();

  42.                 //2 创建TreeMap映射
  43.                 TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();

  44.                 //3 遍历字符数组,计算每个字母出现的次数,并存入TreeMap
  45.                 traverseArray_2(tm,chs);

  46.                 //用StringBuilder存储指定字符串
  47.                 StringBuilder sb = new StringBuilder();

  48.                 //4 将Treemap映射中的键值取出来,以指定字符串的形式返回
  49.                 return        getKeyValue(tm,sb);

  50.         }

  51.         //3.1优化前: 定义方法:遍历字符数组,计算每个字母出现的次数,并存入TreeMap
  52.         public static void traverseArray(TreeMap<Character,Integer> tm,char[] chs)
  53.         {
  54.                 int count = 0;
  55.                 for(int i = 0;i < chs.length; i++)
  56.                 {
  57.                         //如果不是字母则跳过
  58.                         if(!(chs[i] >='a' && chs[i] <= 'z'|| chs[i] >= 'A' && chs[i] <= 'Z'))
  59.                                 continue;
  60.                
  61.                         if(tm.get(chs[i])==null)
  62.                         {
  63.                                 count = 1;
  64.                                 tm.put(chs[i],count);
  65.                         }
  66.                         else
  67.                         {
  68.                                 count = tm.get(chs[i]);
  69.                                 count++;
  70.                                 tm.put(chs[i],count);
  71.                         }
  72.                         count = 0;
  73.                 }
  74.         }

  75.         //3.2优化后 定义方法:遍历字符数组,计算每个字母出现的次数,并存入TreeMap
  76.         public static void traverseArray_2(TreeMap<Character,Integer> tm,char[] chs)
  77.         {
  78.                 int count = 0;
  79.                 for(int i = 0;i < chs.length; i++)
  80.                 {
  81.                         //如果不是字母则跳过
  82.                         if(!(chs[i] >='a' && chs[i] <= 'z'|| chs[i] >= 'A' && chs[i] <= 'Z'))
  83.                                 continue;

  84.                         //优化代码
  85.                         if(tm.get(chs[i]) != null)
  86.                                 count = tm.get(chs[i]);
  87.                         count++;
  88.                         tm.put(chs[i],count);//直接往集合中存储字符和数字,为什么可以,因为自动装箱。
  89.                         count = 0;
  90.                 }        
  91.         }

  92.         //4.1 第一种方式: 将Treemap映射中的键值取出来,以指定字符串的形式返回
  93.         public static String getKeyValue(TreeMap<Character,Integer> tm,StringBuilder sb)
  94.         {
  95.                 Set<Character> keySet = tm.keySet();
  96.                 for(Iterator<Character> it = keySet.iterator(); it.hasNext(); )
  97.                 {
  98.                         Character ch = it.next();
  99.                         Integer i = tm.get(ch);

  100.                         sb.append(ch+"("+i+")");
  101.                 }
  102.                 return sb.toString();        
  103.         }

  104.         //4.2 第二种方式: 将Treemap映射中的键值取出来,以指定字符串的形式返回
  105.         public static String getKeyValue_2(TreeMap<Character,Integer> tm,StringBuilder sb)
  106.         {
  107.                 //第二种方式取出所有键值
  108.                 Set<Map.Entry<Character,Integer>> entry = tm.entrySet();
  109.                 for(Iterator<Map.Entry<Character,Integer>> it = entry.iterator(); it.hasNext(); )
  110.                 {
  111.                         Map.Entry<Character,Integer> me = it.next();
  112.                         Character ch = me.getKey();
  113.                         Integer i = me.getValue();

  114.                         sb.append(ch+"("+i+")");
  115.                 }
  116.                 return sb.toString();
  117.         }
  118. }
复制代码


TreeMap字符串次数演示.jpg (49.97 KB, 下载次数: 16)

TreeMap字符串次数演示.jpg





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2