All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xxl.mathematica.DeleteDuplicatesBy Maven / Gradle / Ivy

package xxl.mathematica;

import xxl.mathematica.predication.FreeQ;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Function;

/**
 * 根据指定条件删除重复元素
 */

public class DeleteDuplicatesBy {
    /**
     * @param list     列表
     * @param function 条件函数
     * @param test     相等函数
     * @param 
     * @param 
     * @return
     */
    public static  List deleteDuplicatesBy(List list, Function function, BiPredicate test) {
        ObjectHelper.requireNonNull(list, function, test);
        if (list.size() == 0) {
            return new ArrayList<>();
        } else {
            List result = new ArrayList<>();
            result.add(list.get(0));//第一个元素肯定不和其他元素重复
            for (int i = 1; i < list.size(); i++) {
                if (FreeQ.freeQ(list, list.get(i), new BiPredicate() {
                    @Override
                    public boolean test(T t, T t2) {
                        return test.test(function.apply(t), function.apply(t2));
                    }
                })) {
                    result.add(list.get(i));
                }
            }
            return result;
        }
    }

    /**
     * 使用equals判断相等性
     *
     * @param list
     * @param function
     * @param 
     * @param 
     * @return
     */
    public static  List deleteDuplicatesBy(List list, Function function) {
        return deleteDuplicatesBy(list, function, new BiPredicate() {
            @Override
            public boolean test(R r, R r2) {
                return r.equals(r2);
            }
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy