WordFilter { @SuppressWarnings("unchecked") public String getKeyword(final String statement) { if (StringUtils.isEmpty(statement)) { return statement; } char[] charArray = statement.toCharArray(); int len = charArray.length; int i = 0; while (i < len) { int end = -1; int index; Map<Character, Map> sub = filterMap; for (index = i; index < len; index++) { sub = sub.get(charArray[index]); if (sub == null) { if (end == -1) { i++; break; } else { StringBuilder sb = new StringBuilder(); for (int j = i; j <= end; j++) { sb.append(charArray[j]); } return sb.toString(); } } else { if (sub.containsKey(END_TAG)) { end = index; } } } if (index >= len) { if (end == -1) { i++; } else { StringBuilder sb = new StringBuilder(); for (int j = i; j <= end; j++) { sb.append(charArray[j]); } return sb.toString(); } } } return null; }  @SuppressWarnings("unchecked") void init(final Collection<String> filterWordList); @SuppressWarnings("unchecked") boolean hasFilterWord(final String info); @SuppressWarnings("unchecked") String getFilterString(final String info, final String replaceTag); @SuppressWarnings("unchecked") String getKeyword(final String statement);  }
@Test public void getKeyword() { WordFilter filter = new WordFilter(); Set<String> sss = new HashSet<String>(); sss.add("南京"); sss.add("上海"); sss.add("北京"); sss.add("天津"); sss.add("合肥"); sss.add("深圳"); filter.init(sss); String key = filter.getKeyword("我家住在南京的天安门上"); Assert.equals(key, "南京", ErrorCodeDef.SYSTEM_ERROR_10001); System.out.println("找到了\"南京\"这个关键词"); }