对List去重并保证添加顺序主要有三种方式:
方式一,利用HashSet不能添加重复数据的特性 由于HashSet不能保证添加顺序,所以只能作为判断条件:
private static void removeDuplicate(List<String> list) {
HashSet<String> set = new HashSet<String>(list.size());
List<String> result = new ArrayList<String>(list.size());
for (String str : list) {
if (set.add(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}
方式二,利用LinkedHashSet不能添加重复数据并能保证添加顺序的特性 :
private static void removeDuplicate(List<String> list) {
LinkedHashSet<String> set = new LinkedHashSet<String>(list.size());
set.addAll(list);
list.clear();
list.addAll(set);
}
方式三,利用List的contains方法循环遍历:
private static void removeDuplicate(List<String> list) {
List<String> result = new ArrayList<String>(list.size());
for (String str : list) {
if (!result.contains(str)) {
result.add(str);
}
}
list.clear();
list.addAll(result);
}
准备测试程序:
private static void main(String[] args) {
final List<String> list = new ArrayList<String>();
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));
}