编写java程序,使用map统计某个数组中单词出现的次数
2个回答
展开全部
String[] ks = { "11", "22", "33", "44", "55", "44", "33", "11" };
Map<String, Integer> map = new HashMap<String, Integer>();
for (String s : ks) {
if (map.get(s) != null) {
map.put(s, map.get(s) + 1);
} else {
map.put(s, 1);
}
}
System.out.println("统计输出:");
for (String s : map.keySet()) {
System.out.println(" " + s + " : " + map.get(s));
}
Map<String, Integer> map = new HashMap<String, Integer>();
for (String s : ks) {
if (map.get(s) != null) {
map.put(s, map.get(s) + 1);
} else {
map.put(s, 1);
}
}
System.out.println("统计输出:");
for (String s : map.keySet()) {
System.out.println(" " + s + " : " + map.get(s));
}
2015-11-04
展开全部
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class MapTest {
public static void main(String[] args) {
String[] data = {"hello","world","tt","hello","world","hello","world"};
Map<String,Integer> map = new HashMap<String,Integer>();
for(String str : data){
if(map.containsKey(str)){
map.put(str, map.get(str) + 1);
}else{
map.put(str, 1);
}
}
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Entry ex = (Entry) it.next();
System.out.println(ex.getKey() + ":" + ex.getValue());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询