序言
检视代码时有下面这样一段代码(已脱敏处理),
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;public class RemoveDuplicateDemo {@Data@NoArgsConstructor@AllArgsConstructorstatic class A {private String aliasName;private int age;}@Data@NoArgsConstructor@AllArgsConstructorstatic class B {private String name;private int age;}private static List<B> removeDuplicateOrigin(List<A> list) {List<B> listB = new ArrayList<>();if (CollectionUtils.isNotEmpty(list)) {Set<String> allName = list.stream().map(A::getAliasName).collect(Collectors.toSet());for (String aliasName : allName) {B b = new B();b.setName(aliasName);listB.add(b);}}return listB;}public static void main(String[] args) {List<A> list = Arrays.asList(new A("tom", 15), new A("john", 16), new A("tim", 11), new A("tom", 30));System.out.println(removeDuplicateOrigin(list));}
}
先思考一下,这样写是否有问题?乍一看,功能上好像没有太大问题。
两个对象A和B,把A对象集合中根据别名进行去重放到B对象集合中。
但实际上有好几处可以优化的地方:
- 1)方法第一行先就创建了一个初始容量为10的数组,假设A对象集合是空的,那么你平白无故创建了一个集合没有任何作用。判空逻辑建议提前。
- 2)使用stream流遍历了一遍A对象集合,后面又重新遍历了一遍A集合的结果集,且A对象的所有元素不能赋值到B对象集合。
改进建议
- 1)使用卫语句减少逻辑嵌套;
- 2)使用HashMap减少遍历次数;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import org.apache.commons.collections4.CollectionUtils;import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;public class RemoveDuplicateDemo {@Data@NoArgsConstructor@AllArgsConstructorstatic class A {private String aliasName;private int age;}@Data@NoArgsConstructor@AllArgsConstructorstatic class B {private String name;private int age;}private static List<B> removeDuplicate(List<A> list) {if (CollectionUtils.isEmpty(list)) {return new ArrayList<>();}// 去重return list.stream().filter(distinctKey(A::getAliasName)).map(RemoveDuplicateDemo::buildB).collect(Collectors.toList());}public static void main(String[] args) {List<A> list = Arrays.asList(new A("tom", 15), new A("john", 16), new A("tim", 11), new A("tom", 30));System.out.println(removeDuplicate(list));}private static Predicate<? super A> distinctKey(Function<? super A, ?> key) {Map<Object, Boolean> map = new ConcurrentHashMap<>();return t -> map.putIfAbsent(key.apply(t), Boolean.TRUE) == null;}private static B buildB(A a) {B b = new B();b.setName(a.getAliasName());b.setAge(a.getAge());return b;}
}