linapex的gravatar头像
linapex 2014-03-01 23:22:41

【Java字符串高效率】统计单词统计个数

package action;
import java.util.Arrays;
import java.util.TreeSet;
public class test {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String strs = "ZZZ BBB AAA OOO ZZZ AAA ZZZ BBB AAA ZZZ AAA VVV OOO CCC DDD CCC CCC KKK BBB AAA ZZZ AAA CCC KKK";
		String[] word = strs.split(" ");
		TreeSet<String> set = new TreeSet();//去重复使用TreeSet
		//排序	
		Arrays.sort(word);	
		//计算出现个数
		for (int i = 0; i < word.length; i++) {
			int count = 0;
			for (int j = 0; j < word.length; j++) {
				if (word[i].equals(word[j])) {
					count += 1;
				}	
			}
			set.add(word[i]+":"+count);	
		}
		//去重复
		for(String s:set){
			System.out.println(s);
		}
	}
}

在OS China上看到有人提到这个问题:

 

对String strs = "ZZZ BBB AAA OOO ZZZ AAA ZZZ"计算出现个数,排序去重
有师弟问到这个问题,最快时间里给了他这个答案

效率太低了,我改进了,代码在压缩包里,效率应该高很多倍,值得参考。


最代码官方编辑于2014-3-2 10:59:13


打赏

文件名:code.rar,文件大小:0.809K 下载
  • /
    • /code.txt
最代码最近下载分享源代码列表最近下载
18258232226  LV1 2019年10月8日
denhill  LV1 2016年12月11日
huanhuan124  LV1 2016年6月26日
wzg356  LV18 2015年1月3日
sayHelloWorld  LV22 2014年4月30日
Jordan  LV1 2014年4月2日
sky  LV22 2014年4月1日
coding_man  LV1 2014年3月25日
小小曦  LV1 2014年3月10日
heyin_2011  LV16 2014年3月4日
最代码最近浏览分享源代码列表最近浏览
微信网友_6004879537377280  LV3 2022年11月11日
taoshen95  LV14 2022年3月14日
2898369623  LV1 2021年10月12日
夜晚星空的诗 2021年9月7日
暂无贡献等级
wangxiaolaugh  LV2 2021年4月2日
FshfshFsh  LV2 2021年3月7日
2196316269  LV10 2021年2月24日
litaosb  LV5 2020年12月14日
Kervin  LV11 2020年12月8日
VillainQAQ  LV1 2020年4月5日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友