没什么好说的,我就直接把代码放上面吧:
package com.anduo.map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class HashMapTest2 {
public static void main(String[] args) {
String s = "abbaadfdgdeiivnielo";
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (map.get(c) == null) {
map.put(c, 1);
} else {
Integer in = map.get(c);
map.put(c, in.intValue() + 1);
}
}
Set<Character> set = map.keySet();
for (Character ch : set) {
System.out.println(ch + " " + map.get(ch));
}
System.out.println("----------------------");
//另外一种访问方式
Set<Entry<Character, Integer>> set1 = map.entrySet();
for (Iterator<Entry<Character, Integer>> iterator = set1.iterator(); iterator
.hasNext();) {
Map.Entry<Character, Integer> entry = (Map.Entry<Character, Integer>) iterator
.next();
Character ch = entry.getKey();
Integer integer = entry.getValue();
System.out.println(ch + " " + integer);
}
}
}
已有 0 人发表留言,猛击->>这里<<-参与讨论
ITeye推荐