cn.jiangzeyin.ArraysUtil Maven / Gradle / Ivy
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 extends T[]>) 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 extends T[]>) 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 extends T[]>) 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;
}
}