在第四步中,我们看到,使用排序好的String[] 要比 ArrayList<String> 查找时候快速的多,因为ArrayList<String>的查找复杂度O(n),而排序好的String[]查找复杂度O(1)。
但是我们还是过量依赖String,这个对象本身效率就不高,于是我们需要换成更底层的原始类型进行优化:
package test;
import static util.PrintUtil.print;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
static String[] keys = {"办证", "气枪出售", "裸聊", "裸表演", "土枪卖"};
static String tContent = "再办证顶";
static ArrayList<String> first = new ArrayList<String>();
static String[] sortFirst;
static char[] charFirst;
static HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
static HashMap<String, String[]> sortMap = new HashMap<String, String[]>();
static HashMap<String, char[]> charMap = new HashMap<String, char[]>();
static {
ArrayList<String> temp;
String key, value;
int length;
for (String k : keys) {
if (!first.contains(k.substring(0, 1))) {
first.add(k.substring(0, 1));
}
length = k.length();
for (int i = 1; i < length; i ++) {
key = k.substring(0, i);
value = k.substring(i, i + 1);
if (i == 1 && !first.contains(key)) {
first.add(key);
}
// 有,添加
if (map.containsKey(key)) {
if (!map.get(key).contains(value)) {
map.get(key).add(value);
}
}
// 没有添加
else {
temp = new ArrayList<String>();
temp.add(value);
map.put(key, temp);
}
}
}
sortFirst = first.toArray(new String[first.size()]);
Arrays.sort(sortFirst); // 排序
charFirst = new char[first.size()];
for (int i = 0; i < charFirst.length; i ++) {
charFirst[i] = first.get(i).charAt(0);
}
Arrays.sort(charFirst); // 排序
String[] sortValue;
ArrayList<String> v;
Map.Entry<String, ArrayList<String>> entry;
Iterator<Entry<String, ArrayList<String>>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
entry = (Map.Entry<String, ArrayList<String>>) iter.next();
v = (ArrayList<String>)entry.getValue();
sortValue = v.toArray(new String[v.size()]);
Arrays.sort(sortValue); // 排序
sortMap.put(entry.getKey(), sortValue);
}
char[] charValue;
iter = map.entrySet().iterator();
while (iter.hasNext()) {
entry = (Map.Entry<String, ArrayList<String>>) iter.next();
v = (ArrayList<String>)entry.getValue();
charValue = new char[v.size()];
for (int i = 0; i < charValue.length; i ++) {
charValue[i] = v.get(i).charAt(0);
}
Arrays.sort(charValue); // 排序
charMap.put(entry.getKey(), charValue);
}
}
/**
* 快速实现的方法
*/
public final static String test1(String content) {
for (String k : keys) {
if (content.indexOf(k) > -1)
return k;
}
return null;
}
/**
* 优化一
*/
public final static String test2(String content) {
boolean bFirst = false;
int length = content.length();
for (int i = 0; i < length; i ++) {
if (first.contains(content.substring(i, i + 1))) {
bFirst = true;
break;
}
}
return bFirst ? test1(content) : null;
}
/**
* 优化二
*/
public final static String test3(String content) {
String r = null, f, g, c = content;
ArrayList<String> temps;
int length = c.length();
tag : for (int i = 0; i < length - 1; i++) {
f = c.substring(i, i + 1);
if (first.contains(f)) {
for (int j = i + 1; j < length; j++) {
f = c.substring(i, j);
g = c.substring(j, j + 1);
temps = map.get(f);
if (temps == null) { // 找到了
print("ok");
r = f;
break tag;
}
if (temps.contains(g)) {
if (j == length - 1) {
print("find!");
r = c.substring(i, j + 1);
break tag;
}
} else { // 没有找到了
break;
}
}
}
}
return r;
}
/**
* 优化三
*/
public final static String test4(String content) {
String r = null, f, g, c = content;
String[] temps;
int length = c.length();
tag : for (int i = 0; i < length - 1; i++) {
f = c.substring(i, i + 1);
// 二分查找
if (Arrays.binarySearch(sortFirst, f) > -1) {
for (int j = i + 1; j < length; j++) {
f = c.substring(i, j);
g = c.substring(j, j + 1);
temps = sortMap.get(f);
if (temps == null) { // 找到了
print("ok");
r = f;
break tag;
}
// 二分查找
if (Arrays.binarySearch(temps, g) > -1) {
if (j == length - 1) {
print("find!");
r = c.substring(i, j + 1);
break tag;
}
} else { // 没有找到了
break;
}
}
}
}
return r;
}
/**
* 优化四
*/
public final static String test5(String content) {
String r = null, f, c = content;
char g;
char[] temps;
int length = c.length();
tag : for (int i = 0; i < length - 1; i++) {
g = c.charAt(i);
// 二分查找
if (Arrays.binarySearch(charFirst, g) > -1) {
for (int j = i + 1; j < length; j++) {
f = c.substring(i, j);
g = c.charAt(j);
temps = charMap.get(f);
if (temps == null) { // 找到了
print("ok");
r = f;
break tag;
}
// 二分查找
if (Arrays.binarySearch(temps, g) > -1) {
if (j == length - 1) {
print("find!");
r = c.substring(i, j + 1);
break tag;
}
} else { // 没有找到了
break;
}
}
}
}
return r;
}
public static void main(String[] args) {
long time1 = System.currentTimeMillis();
print(test5(tContent));
print("test5 time:" + (System.currentTimeMillis() - time1));
}
}
来源:oschina
链接:https://my.oschina.net/u/113867/blog/32386