句子中快速找词
public class WordsFinder {/** * 统计word在text中出现的次数 * @Date: 2013-6-14下午05:57:19 * @Description: int * @param word * @param text * @return */public static int count(String word,String text){char[]w = word.toCharArray();char[]t = text.toCharArray();int size = w.length;//List<String> list = new ArrayList<String>();int sum = 0;for (int i = 0; i < t.length; i++) {if(w[0]==t[i]){if(i+size-1<t.length){boolean bool = true;for (int j = 1; j < size; j++) {bool&=(w[j]==t[i+j]);if(!bool){break;}}if(bool){sum++;}}}}return sum;}/** * 查找text中是否包含的list的元素,返回包含的元素 * @Date: 2013-6-27下午02:57:28 * @Description: Set<String> * @param list * @param text * @return */public static Set<String> seek(List<String> list,String text){Set<String> words = new HashSet<String>(list);Set<String> set = new HashSet<String>();for (String word : words) {int sum = count(word, text);if(sum>0){set.add(word);}}return set;}/** * 查找text中是否包含的list的元素,返回包含的元素及数量 * @Date: 2013-6-27下午02:57:33 * @Description: Map<String,Integer> * @param list * @param text * @return */public static Map<String,Integer> seekCount(List<String> list,String text){Set<String> words = new HashSet<String>(list);Map<String,Integer> map = new HashMap<String, Integer>();for (String word : words) {int sum = count(word, text);if(sum>0){map.put(word, sum);}}return map;}}
?