标签:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class demo{
private Map<String, Integer> wordsMap;
public demo(String strWords){
wordsMap=this.getArray(strWords);
}
public Map<String,Integer>getArray(String strWords){
String[] words_Array =strWords.split(" ");
Map<String,Integer>words_Map=new HashMap<String,Integer>();
int arrLength=words_Array.length;
for(int i=0;i<arrLength;i++){
if(!words_Map.containsKey(words_Array[i])){
words_Map.put(words_Array[i],1);
}/*else{
int currentNum=words_Map.get(words_Array[i])+1;
words_Map.remove(words_Array[i]);
words_Map.put(words_Array[i],currentNum);
}*/
}
return words_Map;
}
public void OutputResult(){
Set<String> s=wordsMap.keySet();
Iterator i= s.iterator();
while(i.hasNext()){
Object o=i.next();
System.out.println(o+"出现了次数为"+wordsMap.get(o));
}
}
public static void main(String[] args){
demo d=new demo("hello word word");
d.OutputResult();
}
}
标签:
原文地址:http://www.cnblogs.com/a13857a/p/5343239.html