第一:集合中做字符包含等比较;
十万条查找1万条数据代码如下;
String congt = "XXOO8";
int mis = 100000;
ArrayList<String> arrayList = new ArrayList<>();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < mis; i++) {
arrayList.add("XXOO"+i);
}
long l = System.currentTimeMillis();
for (String str : arrayList) {
for (int i = 0; i < 10000; i++) {
if (str.equals(congt+i)) {
buffer.append(str);
}
}
}
long ll = System.currentTimeMillis();
long equ = ll-l;
System.out.println("使用equals用时:"+ equ);
for (int i = 0; i < 10000; i++) {
if (arrayList.contains(congt+i)) {
buffer.append(congt);
}
}
long lll = System.currentTimeMillis();
long equs = lll-ll;
System.out.println("使用ArrayList包含:"+ equs);
HashSet<String> hashSet = new HashSet<>(arrayList);
for (int i = 0; i < 10000; i++) {
if (hashSet.contains(congt+i)) {
buffer.append(congt);
}
}
long llll = System.currentTimeMillis();
long equss = llll-lll;
System.out.println("使用HashSet包含:"+ equss);
打印:单位毫秒
使用ArrayList做equals用时:70113
使用ArrayList包含:7826
使用HashSet包含:22
由此可见当我们在使用包含是否相等时可以使用HashSet;