mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add filter and filterNew
This commit is contained in:
parent
a61fe1eecb
commit
f76d314fc5
@ -6,6 +6,8 @@
|
||||
## 4.6.5
|
||||
|
||||
### 新特性
|
||||
* 【core】 CollUtil增加filterNew等方法(原filter变更为filterNew,新增filter)
|
||||
|
||||
### Bug修复
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
@ -72,7 +72,7 @@ public class CollUtil {
|
||||
public static <T> Set<T> emptyIfNull(Set<T> set) {
|
||||
return (null == set) ? Collections.<T>emptySet() : set;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 如果提供的集合为{@code null},返回一个不可变的默认空集合,否则返回原集合<br>
|
||||
* 空集合使用{@link Collections#emptyList()}
|
||||
@ -971,7 +971,7 @@ public class CollUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤<br>
|
||||
* 过滤,此方法产生一个新集合<br>
|
||||
* 过滤过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能:
|
||||
*
|
||||
* <pre>
|
||||
@ -1052,7 +1052,7 @@ public class CollUtil {
|
||||
* @return 过滤后的数组
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public static <T> Collection<T> filter(Collection<T> collection, Filter<T> filter) {
|
||||
public static <T> Collection<T> filterNew(Collection<T> collection, Filter<T> filter) {
|
||||
if (null == collection || null == filter) {
|
||||
return collection;
|
||||
}
|
||||
@ -1087,7 +1087,7 @@ public class CollUtil {
|
||||
* @return 过滤后的数组
|
||||
* @since 4.1.8
|
||||
*/
|
||||
public static <T> List<T> filter(List<T> list, Filter<T> filter) {
|
||||
public static <T> List<T> filterNew(List<T> list, Filter<T> filter) {
|
||||
if (null == list || null == filter) {
|
||||
return list;
|
||||
}
|
||||
@ -1101,64 +1101,81 @@ public class CollUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除{@code null} 元素
|
||||
*
|
||||
* @param collection 集合
|
||||
* @return 处理后的集合
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T> Collection<T> removeNull(Collection<T> collection) {
|
||||
return filter(collection, new Editor<T>() {
|
||||
@Override
|
||||
public T edit(T t) {
|
||||
// 返回null便不加入集合
|
||||
return t;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 去掉集合中的多个元素
|
||||
* 去掉集合中的多个元素,此方法直接修改原集合
|
||||
*
|
||||
* @param <T> 集合类型
|
||||
* @param <E> 集合元素类型
|
||||
* @param collection 集合
|
||||
* @param elesRemoved 被去掉的元素数组
|
||||
* @return 原集合
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Collection<T> removeAny(Collection<T> collection, T... elesRemoved) {
|
||||
public static <T extends Collection<E>, E> T removeAny(T collection, E... elesRemoved) {
|
||||
collection.removeAll(newHashSet(elesRemoved));
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除{@code null}或者"" 元素
|
||||
* 去除指定元素,此方法直接修改原集合
|
||||
*
|
||||
* @param <T> 集合类型
|
||||
* @param <E> 集合元素类型
|
||||
* @param collection 集合
|
||||
* @param filter 过滤器
|
||||
* @return 处理后的集合
|
||||
* @since 4.6.5
|
||||
*/
|
||||
public static <T extends Collection<E>, E> T filter(T collection, final Filter<E> filter) {
|
||||
return IterUtil.filter(collection, filter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除{@code null} 元素,此方法直接修改原集合
|
||||
*
|
||||
* @param <T> 集合类型
|
||||
* @param <E> 集合元素类型
|
||||
* @param collection 集合
|
||||
* @return 处理后的集合
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T extends CharSequence> Collection<T> removeEmpty(Collection<T> collection) {
|
||||
return filter(collection, new Filter<T>() {
|
||||
public static <T extends Collection<E>, E> T removeNull(T collection) {
|
||||
return filter(collection, new Filter<E>() {
|
||||
@Override
|
||||
public boolean accept(T t) {
|
||||
return false == StrUtil.isEmpty(t);
|
||||
public boolean accept(E e) {
|
||||
return null != e;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除{@code null}或者""或者空白字符串 元素
|
||||
* 去除{@code null}或者"" 元素,此方法直接修改原集合
|
||||
*
|
||||
* @param collection 集合
|
||||
* @return 处理后的集合
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T extends CharSequence> Collection<T> removeBlank(Collection<T> collection) {
|
||||
return filter(collection, new Filter<T>() {
|
||||
public static <T extends Collection<E>, E extends CharSequence> T removeEmpty(T collection) {
|
||||
return filter(collection, new Filter<E>() {
|
||||
@Override
|
||||
public boolean accept(T t) {
|
||||
return false == StrUtil.isBlank(t);
|
||||
public boolean accept(E e) {
|
||||
return StrUtil.isNotEmpty(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除{@code null}或者""或者空白字符串 元素,此方法直接修改原集合
|
||||
*
|
||||
* @param collection 集合
|
||||
* @return 处理后的集合
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T extends Collection<E>, E extends CharSequence> T removeBlank(T collection) {
|
||||
return filter(collection, new Filter<E>() {
|
||||
@Override
|
||||
public boolean accept(E e) {
|
||||
return StrUtil.isNotBlank(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import cn.hutool.core.lang.Filter;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
@ -466,6 +467,9 @@ public class IterUtil {
|
||||
* @since 4.0.6
|
||||
*/
|
||||
public static <E> List<E> toList(Iterable<E> iter) {
|
||||
if(null == iter) {
|
||||
return null;
|
||||
}
|
||||
return toList(iter.iterator());
|
||||
}
|
||||
|
||||
@ -523,10 +527,10 @@ public class IterUtil {
|
||||
* @return 第一个元素
|
||||
*/
|
||||
public static <T> T getFirst(Iterable<T> iterable) {
|
||||
if (null != iterable) {
|
||||
return getFirst(iterable.iterator());
|
||||
if (null == iterable) {
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
return getFirst(iterable.iterator());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -578,4 +582,56 @@ public class IterUtil {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤集合,此方法在原集合上直接修改<br>
|
||||
* 通过实现Filter接口,完成元素的过滤,这个Filter实现可以实现以下功能:
|
||||
*
|
||||
* <pre>
|
||||
* 1、过滤出需要的对象,{@link Filter#accept(Object)}方法返回false的对象将被使用{@link Iterator#remove()}方法移除
|
||||
* </pre>
|
||||
*
|
||||
* @param <T> 集合类型
|
||||
* @param <E> 集合元素类型
|
||||
* @param iter 集合
|
||||
* @param filter 过滤器接口
|
||||
* @return 编辑后的集合
|
||||
* @since 4.6.5
|
||||
*/
|
||||
public static <T extends Iterable<E>, E> T filter(T iter, Filter<E> filter) {
|
||||
if(null == iter) {
|
||||
return null;
|
||||
}
|
||||
|
||||
filter(iter.iterator(), filter);
|
||||
|
||||
return iter;
|
||||
}
|
||||
|
||||
/**
|
||||
* 过滤集合,此方法在原集合上直接修改<br>
|
||||
* 通过实现Filter接口,完成元素的过滤,这个Filter实现可以实现以下功能:
|
||||
*
|
||||
* <pre>
|
||||
* 1、过滤出需要的对象,{@link Filter#accept(Object)}方法返回false的对象将被使用{@link Iterator#remove()}方法移除
|
||||
* </pre>
|
||||
*
|
||||
* @param <E> 集合元素类型
|
||||
* @param iter 集合
|
||||
* @param filter 过滤器接口
|
||||
* @return 编辑后的集合
|
||||
* @since 4.6.5
|
||||
*/
|
||||
public static <E> Iterator<E> filter(Iterator<E> iter, Filter<E> filter) {
|
||||
if (null == iter || null == filter) {
|
||||
return iter;
|
||||
}
|
||||
|
||||
while(iter.hasNext()) {
|
||||
if(false == filter.accept(iter.next())) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
return iter;
|
||||
}
|
||||
}
|
||||
|
@ -914,4 +914,29 @@ public class MapUtil {
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除Map中值为{@code null}的键值对<br>
|
||||
* 注意:此方法在传入的Map上直接修改。
|
||||
*
|
||||
* @param map Map
|
||||
* @return map
|
||||
* @since 4.6.5
|
||||
*/
|
||||
public static <K, V> Map<K, V> removeNullValue(Map<K, V> map) {
|
||||
if (isEmpty(map)) {
|
||||
return map;
|
||||
}
|
||||
|
||||
final Iterator<Entry<K, V>> iter = map.entrySet().iterator();
|
||||
Entry<K, V> entry;
|
||||
while(iter.hasNext()) {
|
||||
entry = iter.next();
|
||||
if(null == entry.getValue()) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import cn.hutool.core.collection.CollUtil.Hash;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.lang.Editor;
|
||||
import cn.hutool.core.lang.Filter;
|
||||
import cn.hutool.core.lang.Matcher;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
|
||||
@ -212,6 +213,56 @@ public class CollUtilTest {
|
||||
Assert.assertEquals(CollUtil.newArrayList("a1", "b1", "c1"), filtered);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterTest2() {
|
||||
ArrayList<String> list = CollUtil.newArrayList("a", "b", "c");
|
||||
|
||||
ArrayList<String> filtered = CollUtil.filter(list, new Filter<String>() {
|
||||
|
||||
@Override
|
||||
public boolean accept(String t) {
|
||||
return false == "a".equals(t);
|
||||
}
|
||||
});
|
||||
|
||||
// 原地过滤
|
||||
Assert.assertTrue(list == filtered);
|
||||
Assert.assertEquals(CollUtil.newArrayList("b", "c"), filtered);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeNullTest() {
|
||||
ArrayList<String> list = CollUtil.newArrayList("a", "b", "c", null, "", " ");
|
||||
|
||||
ArrayList<String> filtered = CollUtil.removeNull(list);
|
||||
|
||||
// 原地过滤
|
||||
Assert.assertTrue(list == filtered);
|
||||
Assert.assertEquals(CollUtil.newArrayList("a", "b", "c", "", " "), filtered);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeEmptyTest() {
|
||||
ArrayList<String> list = CollUtil.newArrayList("a", "b", "c", null, "", " ");
|
||||
|
||||
ArrayList<String> filtered = CollUtil.removeEmpty(list);
|
||||
|
||||
// 原地过滤
|
||||
Assert.assertTrue(list == filtered);
|
||||
Assert.assertEquals(CollUtil.newArrayList("a", "b", "c", " "), filtered);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeBlankTest() {
|
||||
ArrayList<String> list = CollUtil.newArrayList("a", "b", "c", null, "", " ");
|
||||
|
||||
ArrayList<String> filtered = CollUtil.removeBlank(list);
|
||||
|
||||
// 原地过滤
|
||||
Assert.assertTrue(list == filtered);
|
||||
Assert.assertEquals(CollUtil.newArrayList("a", "b", "c"), filtered);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groupTest() {
|
||||
List<String> list = CollUtil.newArrayList("1", "2", "3", "4", "5", "6");
|
||||
@ -576,11 +627,11 @@ public class CollUtilTest {
|
||||
public void zipTest() {
|
||||
Collection<String> keys = CollUtil.newArrayList("a", "b", "c", "d");
|
||||
Collection<Integer> values = CollUtil.newArrayList(1, 2, 3, 4);
|
||||
|
||||
|
||||
Map<String, Integer> map = CollUtil.zip(keys, values);
|
||||
|
||||
|
||||
Assert.assertEquals(4, map.size());
|
||||
|
||||
|
||||
Assert.assertEquals(1, map.get("a").intValue());
|
||||
Assert.assertEquals(2, map.get("b").intValue());
|
||||
Assert.assertEquals(3, map.get("c").intValue());
|
||||
|
Loading…
Reference in New Issue
Block a user