使用Set类系下的HashSet和TreeSet来进行去重。
首先判断需要进行去重的数据类型其是否实现了Comparable<T>接口。如果有,则判断其是否符合去重需求;如果不符合,则可以通过TreeSet创建自定义去重方式的Set对象。
// 普通方式Set<Test> idSet = new TreeSet<>(new Comparator<Test>() {public int compare(Test t1, Test t2) {return t1.id.intValue() - t2.id.intValue();}});// lambda表达式idSet = new TreeSet<>((t1, t2) ->t1.id.intValue() - t2.id.intValue());