对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 removeDuplicate2(List<String> list) { LinkedHashSet<String> set = new LinkedHashSet<String>(list.size()); set.addAll(list); list.clear(); list.addAll(set); }
方式三,利用List的contains方法循环遍历:
private static void removeDuplicate3(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); }
测试方法:
public 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)); }
测试结果:
time1:451
time2:579
time3:62113
结论:
推荐用方法一或方法二的方式进行去重
原文地址:https://www.cnblogs.com/tc310/p/9304634.html
时间: 2024-10-08 01:41:58