日期:2014-05-18 浏览次数:20772 次
package test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
// import java.util.TreeMap;
public class HashMapTest
{
public static void main(String[] args)
{
HashMap<Integer,String> hm = new HashMap<Integer,String>();
hm.put(1, "liudehua");
hm.put(3, "zhangxueyou");
hm.put(2, "liming");
hm.put(4, "guofucheng");
Iterator<Map.Entry<Integer, String>> it =
hm.entrySet().iterator();
while(it.hasNext())
{
Map.Entry<Integer, String> m = it.next();
System.out.println(m.getKey()+":"+m.getValue());
// System.out.println(it.next().ge);
}
String str =
"abcdsafasfdasfasfdasdfasfdsfaf";
Map<Character,Integer> mapCI = new HashMap<Character,Integer>();
for(int i = 1;i<str.length();i++)
{
if(!mapCI.containsKey(str.charAt(i)))
mapCI.put(str.charAt(i), 1);
else
mapCI.put(str.charAt(i),mapCI.get(str.charAt(i))+1);
}
Iterator<Map.Entry<Character, Integer>> it2
= mapCI.entrySet().iterator();
while(it2.hasNext())
{
Map.Entry<Character, Integer> m = it2.next();
System.out.println(m.getKey()+" : " + m.getValue());
}
}
}