xxl.mathematica.DeleteDuplicates Maven / Gradle / Ivy
package xxl.mathematica;
import xxl.mathematica.predication.FreeQ;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;
/**
* 删除重复元素
*/
public class DeleteDuplicates {
/**
* 将 test 应用到元素对中,确定它们是否是重复的
* 不对元素重排序,仅删除它们
*
* @param list
* @param test
* @param
* @return
*/
public static List deleteDuplicates(List list, BiPredicate test) {
ObjectHelper.requireNonNull(list, test);
if (list.size() == 0 || list.size() == 1) {//一个元素不存在重复值
return new ArrayList<>(list);
} else {
List result = new ArrayList<>();
result.add(list.get(0));
for (int i = 1; i < list.size(); i++) {
if (FreeQ.freeQ(result, list.get(i), test)) {
result.add(list.get(i));
}
}
return result;
}
}
/**
* 使用equals判断相等性
*
* @param list
* @param
* @return
*/
public static List deleteDuplicates(List list) {
return deleteDuplicates(list, new BiPredicate() {
@Override
public boolean test(T t, T t2) {
return t.equals(t2);
}
});
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy