mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
commit
dfa0191b36
@ -474,4 +474,19 @@ public class ListUtil {
|
|||||||
public static <T> List<T> empty() {
|
public static <T> List<T> empty() {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 像java11一样获取一个List
|
||||||
|
* @param ts 对象
|
||||||
|
* @param <T> 对象类型
|
||||||
|
* @return 不可修改List
|
||||||
|
*/
|
||||||
|
public static <T> List<T> of(T... ts) {
|
||||||
|
if (ArrayUtil.isEmpty(ts)) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
List<T> unmodifiableList = new ArrayList<>(ts.length);
|
||||||
|
Collections.addAll(unmodifiableList, ts);
|
||||||
|
return Collections.unmodifiableList(unmodifiableList);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import java.util.Comparator;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 比较器链。此链包装了多个比较器,最终比较结果按照比较器顺序综合多个比较器结果。<br>
|
* 比较器链。此链包装了多个比较器,最终比较结果按照比较器顺序综合多个比较器结果。<br>
|
||||||
@ -81,6 +82,26 @@ public class ComparatorChain<E> implements Chain<Comparator<E>, ComparatorChain<
|
|||||||
orderingBits = bits;
|
orderingBits = bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <E> ComparatorChain<E> of(Comparator<E> comparator) {
|
||||||
|
return of(comparator, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E> ComparatorChain<E> of(Comparator<E> comparator, boolean reverse) {
|
||||||
|
return new ComparatorChain<>(comparator, reverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E> ComparatorChain<E> of(Comparator<E>... comparators) {
|
||||||
|
return of(Arrays.asList(comparators));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E> ComparatorChain<E> of(List<Comparator<E>> comparators) {
|
||||||
|
return new ComparatorChain<>(comparators);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E> ComparatorChain<E> of(List<Comparator<E>> comparators, BitSet bits) {
|
||||||
|
return new ComparatorChain<>(comparators, bits);
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* 在链的尾部添加比较器,使用正向排序
|
* 在链的尾部添加比较器,使用正向排序
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package cn.hutool.core.comparator;
|
package cn.hutool.core.comparator;
|
||||||
|
|
||||||
|
import java.text.Collator;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 比较工具类
|
* 比较工具类
|
||||||
@ -108,4 +112,34 @@ public class CompareUtil {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中文比较器
|
||||||
|
*
|
||||||
|
* @param keyExtractor 从对象中提取中文
|
||||||
|
* @param <T> 对象类型
|
||||||
|
*
|
||||||
|
* @return 中文比较器
|
||||||
|
*/
|
||||||
|
public static <T> Comparator<T> comparingPinyin(Function<T, String> keyExtractor) {
|
||||||
|
return comparingPinyin(keyExtractor, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中文比较器
|
||||||
|
*
|
||||||
|
* @param keyExtractor 从对象中提取中文
|
||||||
|
* @param reverse 是否反序
|
||||||
|
* @param <T> 对象类型
|
||||||
|
*
|
||||||
|
* @return 中文比较器
|
||||||
|
*/
|
||||||
|
public static <T> Comparator<T> comparingPinyin(Function<T, String> keyExtractor, boolean reverse) {
|
||||||
|
Objects.requireNonNull(keyExtractor);
|
||||||
|
PinyinComparator pinyinComparator = new PinyinComparator();
|
||||||
|
if (reverse) {
|
||||||
|
return (o1, o2) -> pinyinComparator.compare(keyExtractor.apply(o2), keyExtractor.apply(o1));
|
||||||
|
}
|
||||||
|
return (o1, o2) -> pinyinComparator.compare(keyExtractor.apply(o1), keyExtractor.apply(o2));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,10 @@ public class Pair<K, V> extends CloneSupport<Pair<K, V>> implements Serializable
|
|||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <K, V> Pair<K, V> of(K key, V value) {
|
||||||
|
return new Pair<>(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取键
|
* 获取键
|
||||||
* @return 键
|
* @return 键
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package cn.hutool.core.comparator;
|
package cn.hutool.core.comparator;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class CompareUtilTest {
|
public class CompareUtilTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -13,4 +18,21 @@ public class CompareUtilTest {
|
|||||||
compare = CompareUtil.compare(null, "a", false);
|
compare = CompareUtil.compare(null, "a", false);
|
||||||
Assert.assertTrue(compare < 0);
|
Assert.assertTrue(compare < 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void comparingPinyin() {
|
||||||
|
List<String> list = new ArrayList<>();
|
||||||
|
Collections.addAll(list, "成都", "北京", "上海", "深圳");
|
||||||
|
|
||||||
|
List<String> ascendingOrderResult = ListUtil.of("北京", "成都", "上海", "深圳");
|
||||||
|
List<String> descendingOrderResult = ListUtil.of("深圳", "上海", "成都", "北京");
|
||||||
|
|
||||||
|
// 正序
|
||||||
|
list.sort(CompareUtil.comparingPinyin(e -> e));
|
||||||
|
Assert.assertEquals(list, ascendingOrderResult);
|
||||||
|
|
||||||
|
// 反序
|
||||||
|
list.sort(CompareUtil.comparingPinyin(e -> e, true));
|
||||||
|
Assert.assertEquals(list, descendingOrderResult);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user