Java基础之Map学习代码示例二:

import java.util.*;

class MapTest
{
    public static void main(String[] args)
    {
        String tmp = new String("sdfgzxcvasdfxcavdf");
        Map<Character,Integer> map = new TreeMap<Character,Integer>(new Comparator<Character>(){
            public int compare(Character c1,Character c2)
            {
                return c1.compareTo(c2);
            }
        });
        
        for(Character c : tmp.toCharArray())
        {
            if(map.containsKey(c))
            {
                Integer v = map.get(c);
                map.put(c,v+=1);
            }
            else
                map.put(c,1);
        }
        
        for(Iterator<Map.Entry<Character,Integer>> it = map.entrySet().iterator();it.hasNext();)
        {
            Map.Entry<Character,Integer> entry = it.next();
            System.out.print(entry.getKey()+"("+entry.getValue()+") ");
        }
    }
}
原文地址:https://www.cnblogs.com/cxmsky/p/2864760.html