首页 > 编程语言 > 详细

java中通过Map计算重复次数

时间:2016-07-07 02:09:17      阅读:214      评论:0      收藏:0      [点我收藏+]
/**
 * 通过Map计算重复值次数
 */
public class StringUtils {
	
	public static void main(String[] args) {
		
       String s = "长沙,湘潭,湘西,长沙,娄底,株洲,娄底";
       
       String [] array = s.split(",");
       
       Map<Object,Object> map = new HashMap<Object,Object>();      
       
       for(int i = 0; i < array.length; i++) {
    	   if(map.containsKey(array[i])) {
    		   
    		   Integer count = (Integer) map.get(array[i]);
    		   
    		   count++;
    		   
    		   map.put(array[i], count);
    		   
    	   } else {
    		   map.put(array[i], 1);
    	   }
       }
       
       System.out.println(map);
    }
}

? ?使用了Map集合的包含和key唯一

java中通过Map计算重复次数

原文:http://wangduorong.iteye.com/blog/2308976

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!