mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix method
This commit is contained in:
parent
ca2952543a
commit
26064f146c
@ -1141,6 +1141,9 @@ public class CollUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Collection<T> collection2 = ObjectUtil.clone(collection);
|
Collection<T> collection2 = ObjectUtil.clone(collection);
|
||||||
|
if (isEmpty(collection2)) {
|
||||||
|
return collection2;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
collection2.clear();
|
collection2.clear();
|
||||||
} catch (UnsupportedOperationException e) {
|
} catch (UnsupportedOperationException e) {
|
||||||
@ -1176,24 +1179,6 @@ public class CollUtil {
|
|||||||
return edit(collection, t -> filter.accept(t) ? t : null);
|
return edit(collection, t -> filter.accept(t) ? t : null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 过滤<br>
|
|
||||||
* 过滤过程通过传入的Filter实现来过滤返回需要的元素内容,这个Filter实现可以实现以下功能:
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* 1、过滤出需要的对象,{@link Filter#accept(Object)}方法返回true的对象将被加入结果集合中
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @param <T> 集合元素类型
|
|
||||||
* @param list 集合
|
|
||||||
* @param filter 过滤器
|
|
||||||
* @return 过滤后的数组
|
|
||||||
* @since 4.1.8
|
|
||||||
*/
|
|
||||||
public static <T> List<T> filterNew(List<T> list, Filter<T> filter) {
|
|
||||||
return ListUtil.editNew(list, t -> filter.accept(t) ? t : null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 去掉集合中的多个元素,此方法直接修改原集合
|
* 去掉集合中的多个元素,此方法直接修改原集合
|
||||||
*
|
*
|
||||||
|
@ -2,7 +2,6 @@ package cn.hutool.core.collection;
|
|||||||
|
|
||||||
import cn.hutool.core.comparator.PinyinComparator;
|
import cn.hutool.core.comparator.PinyinComparator;
|
||||||
import cn.hutool.core.comparator.PropertyComparator;
|
import cn.hutool.core.comparator.PropertyComparator;
|
||||||
import cn.hutool.core.lang.Editor;
|
|
||||||
import cn.hutool.core.lang.Matcher;
|
import cn.hutool.core.lang.Matcher;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
@ -423,59 +422,6 @@ public class ListUtil {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 编辑列表<br>
|
|
||||||
* 过滤过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* 1、过滤出需要的对象,如果返回null表示这个元素对象抛弃
|
|
||||||
* 2、修改元素对象,返回集合中为修改后的对象
|
|
||||||
* </pre>
|
|
||||||
* 注意:此方法会修改原List!!
|
|
||||||
*
|
|
||||||
* @param <T> 集合元素类型
|
|
||||||
* @param list 集合
|
|
||||||
* @param editor 编辑器接口
|
|
||||||
* @return 过滤后的数组
|
|
||||||
* @since 4.1.8
|
|
||||||
*/
|
|
||||||
public static <T> List<T> editNew(List<T> list, Editor<T> editor) {
|
|
||||||
return (List<T>) CollUtil.edit(list, editor);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 编辑列表<br>
|
|
||||||
* 过滤过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* 1、过滤出需要的对象,如果返回null表示这个元素对象抛弃
|
|
||||||
* 2、修改元素对象,返回集合中为修改后的对象
|
|
||||||
* </pre>
|
|
||||||
* 注意:此方法会修改原List!!
|
|
||||||
*
|
|
||||||
* @param <T> 集合元素类型
|
|
||||||
* @param list 集合
|
|
||||||
* @param editor 编辑器接口
|
|
||||||
* @return 过滤后的数组
|
|
||||||
* @since 4.1.8
|
|
||||||
*/
|
|
||||||
public static <T> List<T> edit(List<T> list, Editor<T> editor) {
|
|
||||||
if (null == list || null == editor) {
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
final int size = list.size();
|
|
||||||
T ele;
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
ele = list.get(i);
|
|
||||||
ele = editor.edit(ele);
|
|
||||||
if(null != ele){
|
|
||||||
list.set(i, ele);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取匹配规则定义中匹配到元素的最后位置<br>
|
* 获取匹配规则定义中匹配到元素的最后位置<br>
|
||||||
* 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。
|
* 此方法对于某些无序集合的位置信息,以转换为数组后的位置为准。
|
||||||
|
@ -609,11 +609,11 @@ public class MapUtil {
|
|||||||
// ----------------------------------------------------------------------------------------------- filter
|
// ----------------------------------------------------------------------------------------------- filter
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 过滤<br>
|
* 编辑Map<br>
|
||||||
* 过滤过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
* 编辑过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* 1、过滤出需要的对象,如果返回null表示这个元素对象抛弃
|
* 1、过滤出需要的对象,如果返回{@code null}表示这个元素对象抛弃
|
||||||
* 2、修改元素对象,返回集合中为修改后的对象
|
* 2、修改元素对象,返回集合中为修改后的对象
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
@ -621,19 +621,24 @@ public class MapUtil {
|
|||||||
* @param <V> Value类型
|
* @param <V> Value类型
|
||||||
* @param map Map
|
* @param map Map
|
||||||
* @param editor 编辑器接口
|
* @param editor 编辑器接口
|
||||||
* @return 过滤后的Map
|
* @return 编辑后的Map
|
||||||
*/
|
*/
|
||||||
public static <K, V> Map<K, V> edit(Map<K, V> map, Editor<Entry<K, V>> editor) {
|
public static <K, V> Map<K, V> edit(Map<K, V> map, Editor<Entry<K, V>> editor) {
|
||||||
if (null == map || null == editor) {
|
if (null == map || null == editor) {
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Map<K, V> map2 = ObjectUtil.clone(map);
|
Map<K, V> map2 = ObjectUtil.clone(map);
|
||||||
if (isEmpty(map2)) {
|
if (isEmpty(map2)) {
|
||||||
return map2;
|
return map2;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
map2.clear();
|
||||||
|
} catch (UnsupportedOperationException e) {
|
||||||
|
// 克隆后的对象不支持清空,说明为不可变集合对象,使用默认的ArrayList保存结果
|
||||||
|
map2 = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
map2.clear();
|
|
||||||
Entry<K, V> modified;
|
Entry<K, V> modified;
|
||||||
for (Entry<K, V> entry : map.entrySet()) {
|
for (Entry<K, V> entry : map.entrySet()) {
|
||||||
modified = editor.edit(entry);
|
modified = editor.edit(entry);
|
||||||
|
@ -575,21 +575,26 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 过滤<br>
|
* 编辑数组<br>
|
||||||
* 过滤过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
* 编辑过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* 1、过滤出需要的对象,如果返回null表示这个元素对象抛弃
|
* 1、过滤出需要的对象,如果返回{@code null}表示这个元素对象抛弃
|
||||||
* 2、修改元素对象,返回集合中为修改后的对象
|
* 2、修改元素对象,返回集合中为修改后的对象
|
||||||
* </pre>
|
* </pre>
|
||||||
|
* <p>
|
||||||
*
|
*
|
||||||
* @param <T> 数组元素类型
|
* @param <T> 数组元素类型
|
||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @param editor 编辑器接口
|
* @param editor 编辑器接口
|
||||||
* @return 过滤后的数组
|
* @since 5.3.3
|
||||||
*/
|
*/
|
||||||
public static <T> T[] filter(T[] array, Editor<T> editor) {
|
public static <T> T[] edit(T[] array, Editor<T> editor) {
|
||||||
ArrayList<T> list = new ArrayList<>(array.length);
|
if (null == editor) {
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
final ArrayList<T> list = new ArrayList<>(array.length);
|
||||||
T modified;
|
T modified;
|
||||||
for (T t : array) {
|
for (T t : array) {
|
||||||
modified = editor.edit(t);
|
modified = editor.edit(t);
|
||||||
@ -597,28 +602,8 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
list.add(modified);
|
list.add(modified);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list.toArray(Arrays.copyOf(array, list.size()));
|
final T[] result = newArray(array.getClass().getComponentType(), list.size());
|
||||||
}
|
return list.toArray(result);
|
||||||
|
|
||||||
/**
|
|
||||||
* 编辑数组<br>
|
|
||||||
* 编辑过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* 1、修改元素对象,返回集合中为修改后的对象
|
|
||||||
* </pre>
|
|
||||||
* <p>
|
|
||||||
* 注意:此方法会修改原数组!
|
|
||||||
*
|
|
||||||
* @param <T> 数组元素类型
|
|
||||||
* @param array 数组
|
|
||||||
* @param editor 编辑器接口
|
|
||||||
* @since 5.3.3
|
|
||||||
*/
|
|
||||||
public static <T> void edit(T[] array, Editor<T> editor) {
|
|
||||||
for (int i = 0; i < array.length; i++) {
|
|
||||||
array[i] = editor.edit(array[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -636,18 +621,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
* @since 3.2.1
|
* @since 3.2.1
|
||||||
*/
|
*/
|
||||||
public static <T> T[] filter(T[] array, Filter<T> filter) {
|
public static <T> T[] filter(T[] array, Filter<T> filter) {
|
||||||
if (null == filter) {
|
return edit(array, t -> filter.accept(t) ? t : null);
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
final ArrayList<T> list = new ArrayList<>(array.length);
|
|
||||||
for (T t : array) {
|
|
||||||
if (filter.accept(t)) {
|
|
||||||
list.add(t);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
final T[] result = newArray(array.getClass().getComponentType(), list.size());
|
|
||||||
return list.toArray(result);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -659,7 +633,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
* @since 3.2.2
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public static <T> T[] removeNull(T[] array) {
|
public static <T> T[] removeNull(T[] array) {
|
||||||
return filter(array, (Editor<T>) t -> {
|
return edit(array, t -> {
|
||||||
// 返回null便不加入集合
|
// 返回null便不加入集合
|
||||||
return t;
|
return t;
|
||||||
});
|
});
|
||||||
@ -697,7 +671,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
* @since 3.2.1
|
* @since 3.2.1
|
||||||
*/
|
*/
|
||||||
public static String[] nullToEmpty(String[] array) {
|
public static String[] nullToEmpty(String[] array) {
|
||||||
return filter(array, (Editor<String>) t -> null == t ? StrUtil.EMPTY : t);
|
return edit(array, t -> null == t ? StrUtil.EMPTY : t);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,7 +40,7 @@ public class ListUtilTest {
|
|||||||
@Test
|
@Test
|
||||||
public void editTest(){
|
public void editTest(){
|
||||||
List<String> a = ListUtil.toLinkedList("1", "2", "3");
|
List<String> a = ListUtil.toLinkedList("1", "2", "3");
|
||||||
final List<String> filter = ListUtil.edit(a, str -> "edit" + str);
|
final List<String> filter = (List<String>) CollUtil.edit(a, str -> "edit" + str);
|
||||||
Assert.assertEquals("edit1", filter.get(0));
|
Assert.assertEquals("edit1", filter.get(0));
|
||||||
Assert.assertEquals("edit2", filter.get(1));
|
Assert.assertEquals("edit2", filter.get(1));
|
||||||
Assert.assertEquals("edit3", filter.get(2));
|
Assert.assertEquals("edit3", filter.get(2));
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package cn.hutool.core.util;
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.lang.Editor;
|
|
||||||
import cn.hutool.core.lang.Filter;
|
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -79,23 +77,23 @@ public class ArrayUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterTest() {
|
public void filterEditTest() {
|
||||||
Integer[] a = {1, 2, 3, 4, 5, 6};
|
Integer[] a = {1, 2, 3, 4, 5, 6};
|
||||||
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2 == 0) ? t : null);
|
Integer[] filter = ArrayUtil.edit(a, t -> (t % 2 == 0) ? t : null);
|
||||||
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
|
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterTestForFilter() {
|
public void filterTestForFilter() {
|
||||||
Integer[] a = {1, 2, 3, 4, 5, 6};
|
Integer[] a = {1, 2, 3, 4, 5, 6};
|
||||||
Integer[] filter = ArrayUtil.filter(a, (Filter<Integer>) t -> t % 2 == 0);
|
Integer[] filter = ArrayUtil.filter(a, t -> t % 2 == 0);
|
||||||
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
|
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void filterTestForEditor() {
|
public void editTest() {
|
||||||
Integer[] a = {1, 2, 3, 4, 5, 6};
|
Integer[] a = {1, 2, 3, 4, 5, 6};
|
||||||
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2 == 0) ? t * 10 : t);
|
Integer[] filter = ArrayUtil.edit(a, t -> (t % 2 == 0) ? t * 10 : t);
|
||||||
Assert.assertArrayEquals(filter, new Integer[]{1, 20, 3, 40, 5, 60});
|
Assert.assertArrayEquals(filter, new Integer[]{1, 20, 3, 40, 5, 60});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user