2, 지도 집합 연습

1217 단어 map
/**
 * “sdfgzxcvasdfxcvdf”               。
 *       :a(1)c(2)
 */
import java.util.*;

class TreeMapDemo 
{
	public static void main(String[] args) 
	{
		String str = "sdfgzxcvasdfxcfvdf";
		TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
		tongJi(str,tm);
		Set<Map.Entry<Character,Integer>> set=tm.entrySet();
		Iterator<Map.Entry<Character,Integer>> it = set.iterator();
		while(it.hasNext())
		{
			Map.Entry<Character,Integer> entry = it.next();
			Character key = entry.getKey();
			Integer value = entry.getValue();
			System.out.print(key+"("+value+")");
		}
	}
	public static void tongJi(String str,TreeMap<Character,Integer> tm)
	{
		char[] chs = str.toCharArray();
		for(int x=0;x<chs.length;x++)
		{
			Integer value = tm.get(chs[x]);
			if(value == null)
			{
				tm.put(chs[x],1);
			}
			else
			{
				value = value + 1;
				tm.put(chs[x],value);
			}
		}
	}
}

좋은 웹페이지 즐겨찾기