mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -838,7 +838,7 @@ public class CollUtil {
|
||||
* @param size 每个段的长度
|
||||
* @return 分段列表
|
||||
*/
|
||||
public static <T> List<List<T>> split(final Collection<T> collection, final int size) {
|
||||
public static <T> List<List<T>> partition(final Collection<T> collection, final int size) {
|
||||
final List<List<T>> result = new ArrayList<>();
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return result;
|
||||
@@ -1305,6 +1305,19 @@ public class CollUtil {
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static <T> int[] indexOfAll(final Collection<T> collection, final Predicate<T> predicate) {
|
||||
return Convert.convert(int[].class, indexListOfAll(collection, predicate));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取匹配规则定义中匹配到元素的所有位置<br>
|
||||
* 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param collection 集合
|
||||
* @param predicate 匹配器,为空则全部匹配
|
||||
* @return 位置数组
|
||||
*/
|
||||
public static <T> List<Integer> indexListOfAll(final Collection<T> collection, final Predicate<T> predicate) {
|
||||
final List<Integer> indexList = new ArrayList<>();
|
||||
if (null != collection) {
|
||||
int index = 0;
|
||||
@@ -1316,7 +1329,7 @@ public class CollUtil {
|
||||
}
|
||||
}
|
||||
|
||||
return Convert.convert(int[].class, indexList);
|
||||
return indexList;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------- zip
|
||||
|
@@ -568,19 +568,6 @@ public class ListUtil {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取匹配规则定义中匹配到元素的所有位置
|
||||
*
|
||||
* @param <T> 元素类型
|
||||
* @param list 列表
|
||||
* @param matcher 匹配器,为空则全部匹配
|
||||
* @return 位置数组
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static <T> int[] indexOfAll(final List<T> list, final Predicate<T> matcher) {
|
||||
return CollUtil.indexOfAll(list, matcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过传入分区长度,将指定列表分区为不同的块,每块区域的长度相同(最后一块可能小于长度)<br>
|
||||
* 分区是在原List的基础上进行的,返回的分区是不可变的抽象列表,原列表元素变更,分区中元素也会变更。
|
||||
@@ -606,33 +593,14 @@ public class ListUtil {
|
||||
: new Partition<>(list, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 对集合按照指定长度分段,每一个段为单独的集合,返回这个集合的列表
|
||||
*
|
||||
* <p>
|
||||
* 需要特别注意的是,此方法调用{@link List#subList(int, int)}切分List,
|
||||
* 此方法返回的是原List的视图,也就是说原List有变更,切分后的结果也会变更。
|
||||
* </p>
|
||||
*
|
||||
* @param <T> 集合元素类型
|
||||
* @param list 列表,为空时返回{@link #empty()}
|
||||
* @param size 每个段的长度,当长度超过list长度时,size按照list长度计算,即只返回一个节点
|
||||
* @return 分段列表
|
||||
* @see #partition(List, int)
|
||||
* @since 5.4.5
|
||||
*/
|
||||
public static <T> List<List<T>> split(final List<T> list, final int size) {
|
||||
return partition(list, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将集合平均分成多个list,返回这个集合的列表
|
||||
* <p>例:</p>
|
||||
* <pre>
|
||||
* ListUtil.splitAvg(null, 3); // []
|
||||
* ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 2); // [[1, 2], [3, 4]]
|
||||
* ListUtil.splitAvg(Arrays.asList(1, 2, 3), 5); // [[1], [2], [3], [], []]
|
||||
* ListUtil.splitAvg(Arrays.asList(1, 2, 3), 2); // [[1, 2], [3]]
|
||||
* ListUtil.avgPartition(null, 3); // []
|
||||
* ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 2); // [[1, 2], [3, 4]]
|
||||
* ListUtil.avgPartition(Arrays.asList(1, 2, 3), 5); // [[1], [2], [3], [], []]
|
||||
* ListUtil.avgPartition(Arrays.asList(1, 2, 3), 2); // [[1, 2], [3]]
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> 集合元素类型
|
||||
@@ -642,7 +610,7 @@ public class ListUtil {
|
||||
* @author lileming
|
||||
* @since 5.7.10
|
||||
*/
|
||||
public static <T> List<List<T>> splitAvg(final List<T> list, final int limit) {
|
||||
public static <T> List<List<T>> avgPartition(final List<T> list, final int limit) {
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return empty();
|
||||
}
|
||||
|
@@ -130,8 +130,8 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
*/
|
||||
public List<V> getValues(final K key) {
|
||||
return CollUtil.getAny(
|
||||
this.values,
|
||||
ListUtil.indexOfAll(this.keys, (ele) -> ObjUtil.equals(ele, key))
|
||||
this.values,
|
||||
CollUtil.indexOfAll(this.keys, (ele) -> ObjUtil.equals(ele, key))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -144,8 +144,8 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
*/
|
||||
public List<K> getKeys(final V value) {
|
||||
return CollUtil.getAny(
|
||||
this.keys,
|
||||
ListUtil.indexOfAll(this.values, (ele) -> ObjUtil.equals(ele, value))
|
||||
this.keys,
|
||||
CollUtil.indexOfAll(this.values, (ele) -> ObjUtil.equals(ele, value))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -253,9 +253,9 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TableMap{" +
|
||||
"keys=" + keys +
|
||||
", values=" + values +
|
||||
'}';
|
||||
"keys=" + keys +
|
||||
", values=" + values +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -317,11 +317,10 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
}
|
||||
|
||||
|
||||
|
||||
@SuppressWarnings("NullableProblems")
|
||||
@Override
|
||||
public V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
|
||||
if(null == remappingFunction){
|
||||
if (null == remappingFunction) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -329,9 +328,9 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
|
||||
for (int i = 0; i < size(); i++) {
|
||||
if (ObjUtil.equals(key, keys.get(i))) {
|
||||
final V newValue = remappingFunction.apply(key, values.get(i));
|
||||
if(null != newValue){
|
||||
if (null != newValue) {
|
||||
lastValue = values.set(i, newValue);
|
||||
} else{
|
||||
} else {
|
||||
removeByIndex(i);
|
||||
// 移除当前元素,下个元素前移
|
||||
i--;
|
||||
|
@@ -297,17 +297,17 @@ public class CollUtilTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitTest() {
|
||||
public void partitionTest() {
|
||||
final List<Integer> list = ListUtil.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
final List<List<Integer>> split = CollUtil.split(list, 3);
|
||||
final List<List<Integer>> split = CollUtil.partition(list, 3);
|
||||
Assert.assertEquals(3, split.size());
|
||||
Assert.assertEquals(3, split.get(0).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void splitTest2() {
|
||||
public void partitionTest2() {
|
||||
final ArrayList<Integer> list = ListUtil.of(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
final List<List<Integer>> split = CollUtil.split(list, Integer.MAX_VALUE);
|
||||
final List<List<Integer>> split = CollUtil.partition(list, Integer.MAX_VALUE);
|
||||
Assert.assertEquals(1, split.size());
|
||||
Assert.assertEquals(9, split.get(0).size());
|
||||
}
|
||||
|
@@ -16,25 +16,25 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
public class ListUtilTest {
|
||||
|
||||
@Test
|
||||
public void splitTest() {
|
||||
List<List<Object>> lists = ListUtil.split(null, 3);
|
||||
public void partitionTest() {
|
||||
List<List<Object>> lists = ListUtil.partition(null, 3);
|
||||
Assert.assertEquals(ListUtil.empty(), lists);
|
||||
|
||||
lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 1);
|
||||
lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 1);
|
||||
Assert.assertEquals("[[1], [2], [3], [4]]", lists.toString());
|
||||
lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 2);
|
||||
lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 2);
|
||||
Assert.assertEquals("[[1, 2], [3, 4]]", lists.toString());
|
||||
lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 3);
|
||||
lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 3);
|
||||
Assert.assertEquals("[[1, 2, 3], [4]]", lists.toString());
|
||||
lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 4);
|
||||
lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 4);
|
||||
Assert.assertEquals("[[1, 2, 3, 4]]", lists.toString());
|
||||
lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 5);
|
||||
lists = ListUtil.partition(Arrays.asList(1, 2, 3, 4), 5);
|
||||
Assert.assertEquals("[[1, 2, 3, 4]]", lists.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void splitBenchTest() {
|
||||
public void partitionBenchTest() {
|
||||
final List<String> list = new ArrayList<>();
|
||||
CollUtil.padRight(list, RandomUtil.randomInt(1000_0000, 1_0000_0000), "test");
|
||||
|
||||
@@ -44,11 +44,11 @@ public class ListUtilTest {
|
||||
final StopWatch stopWatch = new StopWatch();
|
||||
|
||||
stopWatch.start("CollUtil#split");
|
||||
final List<List<String>> CollSplitResult = CollUtil.split(list, size);
|
||||
final List<List<String>> CollSplitResult = CollUtil.partition(list, size);
|
||||
stopWatch.stop();
|
||||
|
||||
stopWatch.start("ListUtil#split");
|
||||
final List<List<String>> ListSplitResult = ListUtil.split(list, size);
|
||||
final List<List<String>> ListSplitResult = ListUtil.partition(list, size);
|
||||
stopWatch.stop();
|
||||
|
||||
Assert.assertEquals(CollSplitResult, ListSplitResult);
|
||||
@@ -58,28 +58,28 @@ public class ListUtilTest {
|
||||
|
||||
@Test
|
||||
public void splitAvgTest() {
|
||||
List<List<Object>> lists = ListUtil.splitAvg(null, 3);
|
||||
List<List<Object>> lists = ListUtil.avgPartition(null, 3);
|
||||
Assert.assertEquals(ListUtil.empty(), lists);
|
||||
|
||||
lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 1);
|
||||
lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 1);
|
||||
Assert.assertEquals("[[1, 2, 3, 4]]", lists.toString());
|
||||
lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 2);
|
||||
lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 2);
|
||||
Assert.assertEquals("[[1, 2], [3, 4]]", lists.toString());
|
||||
lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 3);
|
||||
lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 3);
|
||||
Assert.assertEquals("[[1, 2], [3], [4]]", lists.toString());
|
||||
lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 4);
|
||||
lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 4);
|
||||
Assert.assertEquals("[[1], [2], [3], [4]]", lists.toString());
|
||||
|
||||
lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3), 5);
|
||||
lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3), 5);
|
||||
Assert.assertEquals("[[1], [2], [3], [], []]", lists.toString());
|
||||
lists = ListUtil.splitAvg(Arrays.asList(1, 2, 3), 2);
|
||||
lists = ListUtil.avgPartition(Arrays.asList(1, 2, 3), 2);
|
||||
Assert.assertEquals("[[1, 2], [3]]", lists.toString());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void splitAvgNotZero() {
|
||||
// limit不能小于等于0
|
||||
ListUtil.splitAvg(Arrays.asList(1, 2, 3, 4), 0);
|
||||
ListUtil.avgPartition(Arrays.asList(1, 2, 3, 4), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -650,7 +650,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
||||
public void testListSplit() {
|
||||
final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
|
||||
List<List<Integer>> lists = wrap(list).split(2).map(TerminableWrappedStream::toList).toList();
|
||||
Assert.assertEquals(ListUtil.split(list, 2), lists);
|
||||
Assert.assertEquals(ListUtil.partition(list, 2), lists);
|
||||
|
||||
// 指定长度 大于等于 列表长度
|
||||
lists = wrap(list).split(list.size()).map(TerminableWrappedStream::toList).toList();
|
||||
@@ -661,7 +661,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
||||
public void testSplitList() {
|
||||
final List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
|
||||
List<List<Integer>> lists = wrap(list).splitList(2).toList();
|
||||
Assert.assertEquals(ListUtil.split(list, 2), lists);
|
||||
Assert.assertEquals(ListUtil.partition(list, 2), lists);
|
||||
|
||||
// 指定长度 大于等于 列表长度
|
||||
lists = wrap(list).splitList(list.size()).toList();
|
||||
|
Reference in New Issue
Block a user