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

cn.jiangzeyin.ArraysUtil Maven / Gradle / Ivy

There is a newer version: 1.0.13
Show newest version
package cn.jiangzeyin;

import java.util.*;

/**
 * 常用数组操作
 * Created by jiangzeyin on 2017/12/12.
 */
public final class ArraysUtil {
    /**
     * 使用treeSet 方式数组去重
     *
     * @param objects    objects
     * @param comparator 自定义排序接口
     * @param         泛型
     * @return 结果数组 自动排序
     */
    public static  T[] deDupeArrayByTreeSet(T[] objects, Comparator comparator) {
        TreeSet tTreeSet = new TreeSet<>(comparator);
        tTreeSet.addAll(Arrays.asList(objects));
        return Arrays.copyOf(tTreeSet.toArray(), tTreeSet.size(), (Class) objects.getClass());
    }

    /**
     * 使用treeSet 方式数组去重
     *
     * @param objects objects
     * @param      泛型
     * @return 结果数组 自动排序
     */
    public static  T[] deDupeArrayByTreeSet(T[] objects) {
        List list = deDupeArrayByTreeSet(Arrays.asList(objects));
        return Arrays.copyOf(list.toArray(), list.size(), (Class) objects.getClass());
    }

    public static  List deDupeArrayByTreeSet(List list) {
        TreeSet tTreeSet = new TreeSet<>(list);
        list.clear();
        list.addAll(tTreeSet);
        return list;
    }

    /**
     * 使用HashSet 方式数组去重
     *
     * @param objects objects
     * @param      泛型
     * @return 结果数组
     */
    public static  T[] deDupeArrayByHashSet(T[] objects) {
        List list = deDupeArrayByHashSet(Arrays.asList(objects));
        return Arrays.copyOf(list.toArray(), list.size(), (Class) objects.getClass());
    }

    /**
     * 数组去重
     *
     * @param list list
     * @param   泛型
     * @return 新的集合
     */
    public static  List deDupeArrayByHashSet(List list) {
        HashSet tTreeSet = new HashSet<>(list);
        list.clear();
        list.addAll(tTreeSet);
        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy