import java.util.HashMap; import java.util.Map; public class Test { public static void main(String[] args) { Map<String,Integer> map=new HashMap<String,Integer>(); String str="I love you, do you love me? oh,you don't love me"; String[] strs=str.split("\\s|,|\\?|\\."); for(String key:strs){ if(!"".endsWith(key)){ if(map.isEmpty()||!map.containsKey(key)) map.put(key,1); else map.put(key,map.get(key)+1); } } System.out.print(map); } }
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args)
{
Map<String,Integer> map=new HashMap<String,Integer>();
String str="I love you, do you love me? oh,you don't love me";
String[] strs=str.split("\\s|,|\\?|\\.");
for(String key:strs){
if(!"".endsWith(key)){
if(map.isEmpty()||!map.containsKey(key))
map.put(key,1);
else
map.put(key,map.get(key)+1);
}
}
System.out.print(map);
}
}
http://topic.csdn.net/u/20091028/16/d213cbb8-3464-45c9-ba6c-aa6da559be84.html
public class FileStatistician { private static int singleCount = 0;//单独出现的次数 public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile("D:\\Hello.txt","r"); Scanner scan = new Scanner(raf.getChannel()); HashMap<String,Integer> cache = new HashMap<String,Integer>(); while(scan.hasNext()){ String word = scan.next(); Integer count = cache.get(word); if(count==null)count=0; cache.put(word, count+1); } ArrayList<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(cache.entrySet()); if(list.get(0).getValue()==1)singleCount++; Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){ public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { if(o2.getValue()==1)singleCount++; return -o1.getValue().compareTo(o2.getValue()); } }); System.out.println("一共出现了 "+list.size()+" 个单词"); System.out.println("有 "+singleCount+" 个单词只出现了一次"); for(Map.Entry<String, Integer> e : list){ System.out.println(e.getKey()+"\t"+e.getValue()); } } }见贴:
http://topic.csdn.net/u/20091028/16/d213cbb8-3464-45c9-ba6c-aa6da559be84.html