List去重性能比较

/**
* 使用两个for循环实现List去重(有序)
*
* @param list
*/
public static List removeDuplicationBy2For(List<Temp> list) {
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
if (list.get(i).equals(list.get(j))) {
list.remove(j);
}
}
}
return list;
}

/**
* 使用List集合contains方法循环遍历(有序)
*
* @param list
*/
public static List removeDuplicationByContains(List<Temp> list) {
List<Temp> newList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
boolean isContains = newList.contains(list.get(i));
if (!isContains) {
newList.add(list.get(i));
}
}
list.clear();
list.addAll(newList);
return list;
}

/**
* 使用HashSet实现List去重(无序)
*
* @param list
*/
public static List removeDuplicationByHashSet(List<Temp> list) {
HashSet set = new HashSet(list);
//把List集合所有元素清空
list.clear();
//把HashSet对象添加至List集合
list.addAll(set);
return list;
}

/**
* 使用TreeSet实现List去重(有序)
*
* @param list
*/
public static List removeDuplicationByTreeSet(List<Temp> list) {
TreeSet set = new TreeSet(list);
//把List集合所有元素清空
list.clear();
//把HashSet对象添加至List集合
list.addAll(set);
return list;
}

/**
* 使用java8新特性stream实现List去重(有序)
*
* @param list
*/
public static List removeDuplicationByStream(List<Temp> list) {
List newList = list.stream().distinct().collect(Collectors.toList());
return newList;
}

@Test
public void test() throws IOException {
List<Temp> list1 = new ArrayList<>();
List<Temp> list2 = new ArrayList<>();
List<Temp> list3 = new ArrayList<>();
List<Temp> list4 = new ArrayList<>();
List<Temp> list5 = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 10_000; i++) {
int value = random.nextInt(100);
list1.add(Temp.builder().age(value).userName(UUID.randomUUID().toString()).build());
list2.add(Temp.builder().age(value).userName(UUID.randomUUID().toString()).build());
list3.add(Temp.builder().age(value).userName(UUID.randomUUID().toString()).build());
list4.add(Temp.builder().age(value).userName(UUID.randomUUID().toString()).build());
list5.add(Temp.builder().age(value).userName(UUID.randomUUID().toString()).build());
}
long startTime;
long endTime;
startTime = System.currentTimeMillis();
removeDuplicationByHashSet(list1);
endTime = System.currentTimeMillis();
System.out.println("使用HashSet实现List去重时间:" + (endTime - startTime) + "毫秒");
startTime = System.currentTimeMillis();
removeDuplicationByTreeSet(list2);
endTime = System.currentTimeMillis();
System.out.println("使用TreeSet实现List去重时间:" + (endTime - startTime) + "毫秒");
startTime = System.currentTimeMillis();
removeDuplicationByStream(list3);
endTime = System.currentTimeMillis();
System.out.println("使用java8新特性stream实现List去重:" + (endTime - startTime) + "毫秒");
startTime = System.currentTimeMillis();
removeDuplicationBy2For(list4);
endTime = System.currentTimeMillis();
System.out.println("使用两个for循环实现List去重:" + (endTime - startTime) + "毫秒");
startTime = System.currentTimeMillis();
removeDuplicationByContains(list5);
endTime = System.currentTimeMillis();
System.out.println("使用List集合contains方法循环遍历:" + (endTime - startTime) + "毫秒");
}
}

@Data
@Builder
class Temp implements Serializable, Comparable<Temp> {
private String userName;
private Integer age;

@Override
public int hashCode() {
return this.userName.hashCode() + this.age.hashCode();
}

@Override
public boolean equals(Object object) {
if (!(object instanceof Temp)) {
return false;
}
return this.userName.equalsIgnoreCase(((Temp) object).getUserName())
&& this.age.equals(((Temp) object).getAge());
}

@Override
public int compareTo(@NotNull() Temp o) {
if (o == null) {
return -1;
}
if (this.age >= o.getAge()) {
return 1;
}
return 0;
}
}

注:HashSet与TreeSet性能最快,相差无几
posted @ 2020-12-01 13:45  9999号打工仔  阅读(193)  评论(0编辑  收藏  举报