对List去重并保证添加顺序主要有三种方式:
方式一,利用HashSet不能添加重复数据的特性 由于HashSet不能保证添加顺序,所以只能作为判断条件:
private static void removeDuplicate(List list) {
HashSet set = new HashSet(list.size());
List result = new ArrayList(list.size());
for (String str : list) {
if (set.add(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}
方式二,利用LinkedHashSet不能添加重复数据并能保证添加顺序的特性 :
private static void removeDuplicate2(List list) {
LinkedHashSet set = new LinkedHashSet(list.size());
set.addAll(list);
list.clear();
list.addAll(set);
}
方式三,利用List的contains方法循环遍历:
private static void removeDuplicate3(List list) {
List result = new ArrayList(list.size());
for (String str : list) {
if (!result.contains(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}
测试方法:
public static void main(String[] args){
final List list = new ArrayList();
for (int i = 0; i < 1000; i++) {
list.add("haha-" + i);
}
long time = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
removeDuplicate(list);
}
long time1 = System.currentTimeMillis();
System.out.println("time1:"+(time1-time));
for (int i = 0; i < 10000; i++) {
removeDuplicate2(list);
}
long time2 = System.currentTimeMillis();
System.out.println("time2:"+(time2-time1));
for (int i = 0; i < 10000; i++) {
removeDuplicate3(list);
}
long time3 = System.currentTimeMillis();
System.out.println("time3:"+(time3-time2));
}
测试结果:
time1:451
time2:579
time3:62113
结论:
推荐用方法一或方法二的方式进行去重