CarinAcosta的头像 CarinAcosta 0 2016-03-02 19:48 1


× 1   

浏览数: 21649

分享时间: 2 年 前

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
 * 统计abacbacdadbc中的每个字母出现的次数,输出格式是:a(4)b(3)c(3)d(2)
 * 选中TreeMap的原因是:key不重复且按顺序排序取出
 * 思路:
 * 1.将字符串abacbacdadbc转换成字符数组
 * 2.分别取字符,与TreeMap中的key进行比较
 *   2.1 如果TreeMap中存在对应的字符,则取出并自增,再存入TreeMap
 *   2.2 如果TreeMap中不存在对应的字符,则直接存入该字符,value=1

public class A3B1C2_TreeMapTest {

	public static void main(String[] args) {

		TreeMap<String, Integer> tm=new TreeMap<>();
		String string="abacbacdadbc";
		char[] ch=string.toCharArray();
		for(char r :ch){
			//tm.put(String.valueOf(r), 1);
			if(compareKey(r, tm))
				int i=tm.get(String.valueOf(r));
				tm.put(String.valueOf(r), i);
			}else {
				tm.put(String.valueOf(r), 1);
		Set<Map.Entry<String, Integer>> entrySet=tm.entrySet();
		Iterator<Map.Entry<String, Integer>> iterator=entrySet.iterator();
			Map.Entry<String, Integer>;
			String key=me.getKey();
			int value=me.getValue();
	public static boolean compareKey(char c,TreeMap<String, Integer> treeMap){
		if (treeMap.containsKey(String.valueOf(c))) {
			return true;
		return false;


  • ossaa的头像 ossaa 2018-09-06 23:03 代码数:0

    Wonderful illustrated information. I thank you about that. No doubt it will be very useful for my future projects. Would like to see some other posts on the same subject! EMS Geräte

  • cixexey 的头像 cixexey 2018-09-17 20:56 代码数:0

    I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the tips

  • pevahanifo的头像 pevahanifo 2018-09-22 19:20 代码数:0

    Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. technology updates

  • banjue的头像 banjue 2017-04-28 16:50 代码数:0


  • viable的头像 viable 2017-05-19 17:16 代码数:0

    太麻烦了.    value 用  三元 运算符 就可以了

  • ossaa的头像 ossaa 2018-08-12 16:31 代码数:0

    I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Thanks...Onlinr Quran lessons

  • cixexey的头像 cixexey 2018-08-20 15:29 代码数:0

    I felt very happy while reading this site. This was really very informative site for me. I really liked it. This was really a cordial post. Thanks a lot!. search engine marketing

  • cixexey的头像 cixexey 2018-08-21 20:02 代码数:0

    The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface.Just Cannabis

  • cixexey的头像 cixexey 2018-08-24 02:05 代码数:0

    this is really nice to read..informative post is very good to read..thanks a lot! jual mobil bekas

  • cixexey的头像 cixexey 2018-08-24 19:16 代码数:0

    Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for.Lawyer