mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
comparingIndexed增加对集合的支持,可变参数不够友好。
说明:生产中,使用数组、集合更多
This commit is contained in:
parent
629507cbaf
commit
eb2c09a9bd
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.comparator;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
@ -236,6 +238,7 @@ public class CompareUtil {
|
||||
/**
|
||||
* 索引比较器<br>
|
||||
* 通过keyExtractor函数,提取对象的某个属性或规则,根据提供的排序数组,完成比较<br>
|
||||
* objs中缺失的,默认排序在前面(atEndIfMiss=false)<br>
|
||||
*
|
||||
* @param keyExtractor 从对象中提取中文(参与比较的内容)
|
||||
* @param objs 参与排序的数组,数组的元素位置决定了对象的排序先后
|
||||
@ -245,17 +248,38 @@ public class CompareUtil {
|
||||
* @since 5.8.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, U> Comparator<T> comparingIndexed(final Function<? super T, ? extends U> keyExtractor, final U... objs) {
|
||||
public static <T, U> Comparator<T> comparingIndexed(final Function<? super T, ? extends U> keyExtractor, final U[] objs) {
|
||||
return comparingIndexed(keyExtractor, false, objs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 索引比较器<br>
|
||||
* 通过keyExtractor函数,提取对象的某个属性或规则,根据提供的排序数组,完成比较<br>
|
||||
* objs中缺失的,默认排序在前面(atEndIfMiss=false)<br>
|
||||
*
|
||||
* @param keyExtractor 从对象中提取中文(参与比较的内容)
|
||||
* @param objs 参与排序的集合对象,数组的元素位置决定了对象的排序先后
|
||||
* @param <T> 对象类型
|
||||
* @param <U> 数组对象类型
|
||||
* @return 索引比较器
|
||||
* @since 6.0.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T, U> Comparator<T> comparingIndexed(final Function<? super T, ? extends U> keyExtractor, final Collection<U> objs) {
|
||||
U[] array = null;
|
||||
if (objs != null && objs.size() > 0) {
|
||||
array = objs.toArray((U[]) Array.newInstance(objs.iterator().next().getClass(), objs.size()));
|
||||
}
|
||||
return comparingIndexed(keyExtractor, false, array);
|
||||
}
|
||||
|
||||
/**
|
||||
* 索引比较器<br>
|
||||
* 通过keyExtractor函数,提取对象的某个属性或规则,根据提供的排序数组,完成比较<br>
|
||||
*
|
||||
* @param keyExtractor 从对象中提取排序键的函数(参与比较的内容)
|
||||
* @param atEndIfMiss 如果不在列表中是否排在后边
|
||||
* @param objs 参与排序的数组,数组的元素位置决定了对象的排序先后
|
||||
* @param atEndIfMiss 如果不在列表中是否排在后边; true:排在后边; false:排在前边
|
||||
* @param objs 参与排序的数组,数组的元素位置决定了对象的排序先后, 示例:<code>int[] objs = new int[]{3, 2, 1, 4, 5,6};</code>
|
||||
* @param <T> 对象类型
|
||||
* @param <U> 数组对象类型
|
||||
* @return 索引比较器
|
||||
|
Loading…
x
Reference in New Issue
Block a user