java中通过Map计算重复次数

/**
 * 通过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唯一

猜你喜欢

转载自wangduorong.iteye.com/blog/2308976