java统计字符串中单词的数目

统计字符串中单词的数目

 

package com.commontest.pro1;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author jiangfuqiang
* @version 2016/6/24.
* @see com.commontest.pro1
*/
public class CountWord {

public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
String data = "Learn the Spring spring - Spring Boot builds on many other Spring projects, check the spring.io web-site for a wealth of reference documentation. If you are just ";
count(map,data,true);
//这里将map.entrySet()转换成list
List<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());
//然后通过比较器来实现排序
Collections.sort(list,new Comparator<Map.Entry<String,Integer>>() {
//升序排序
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return o1.getValue().intValue() > o2.getValue().intValue()?-1:1;
}

});
System.out.println(list.toString());
}

/**
*
* @param map
* @param data
* @param isLower 是否都转换为小写
*/
private static void count(Map<String, Integer> map, String data, boolean isLower) {
String[] datas = data.split("\\s+");
Pattern pattern = Pattern.compile("((^[a-zA-Z]+[\\-]?(\\.[a-zA-Z]+)?[a-zA-Z]*)(\\W)?)");
for (String str : datas) {
Matcher matcher = pattern.matcher(str);

if (matcher.matches()) {
String word = matcher.group(2).trim();
if (isLower) {
word = word.toLowerCase();
}
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);
}
} else {
System.out.println("not word: " + str);
}
}
}

}

  

posted @ 2016-06-24 18:16  jiangfullll  阅读(260)  评论(0)    收藏  举报