From f76d314fc50c3dac2d67147278acc115ead7bb9d Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 30 Aug 2019 11:28:51 +0800 Subject: [PATCH] add filter and filterNew --- CHANGELOG.md | 2 + .../cn/hutool/core/collection/CollUtil.java | 83 +++++++++++-------- .../cn/hutool/core/collection/IterUtil.java | 62 +++++++++++++- .../main/java/cn/hutool/core/map/MapUtil.java | 25 ++++++ .../hutool/core/collection/CollUtilTest.java | 57 ++++++++++++- 5 files changed, 190 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a76973f85..019977385 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ ## 4.6.5 ### 新特性 +* 【core】 CollUtil增加filterNew等方法(原filter变更为filterNew,新增filter) + ### Bug修复 ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 429a7d266..2e0d91a57 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -72,7 +72,7 @@ public class CollUtil { public static Set emptyIfNull(Set set) { return (null == set) ? Collections.emptySet() : set; } - + /** * 如果提供的集合为{@code null},返回一个不可变的默认空集合,否则返回原集合
* 空集合使用{@link Collections#emptyList()} @@ -971,7 +971,7 @@ public class CollUtil { } /** - * 过滤
+ * 过滤,此方法产生一个新集合
* 过滤过程通过传入的Editor实现来返回需要的元素内容,这个Editor实现可以实现以下功能: * *
@@ -1052,7 +1052,7 @@ public class CollUtil {
 	 * @return 过滤后的数组
 	 * @since 3.1.0
 	 */
-	public static  Collection filter(Collection collection, Filter filter) {
+	public static  Collection filterNew(Collection collection, Filter filter) {
 		if (null == collection || null == filter) {
 			return collection;
 		}
@@ -1087,7 +1087,7 @@ public class CollUtil {
 	 * @return 过滤后的数组
 	 * @since 4.1.8
 	 */
-	public static  List filter(List list, Filter filter) {
+	public static  List filterNew(List list, Filter 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  Collection removeNull(Collection collection) {
-		return filter(collection, new Editor() {
-			@Override
-			public T edit(T t) {
-				// 返回null便不加入集合
-				return t;
-			}
-		});
-	}
-
-	/**
-	 * 去掉集合中的多个元素
+	 * 去掉集合中的多个元素,此方法直接修改原集合
 	 * 
+	 * @param  集合类型
+	 * @param  集合元素类型
 	 * @param collection 集合
 	 * @param elesRemoved 被去掉的元素数组
 	 * @return 原集合
 	 * @since 4.1.0
 	 */
 	@SuppressWarnings("unchecked")
-	public static  Collection removeAny(Collection collection, T... elesRemoved) {
+	public static , E> T removeAny(T collection, E... elesRemoved) {
 		collection.removeAll(newHashSet(elesRemoved));
 		return collection;
 	}
 
 	/**
-	 * 去除{@code null}或者"" 元素
+	 * 去除指定元素,此方法直接修改原集合
 	 * 
+	 * @param  集合类型
+	 * @param  集合元素类型
+	 * @param collection 集合
+	 * @param filter 过滤器
+	 * @return 处理后的集合
+	 * @since 4.6.5
+	 */
+	public static , E> T filter(T collection, final Filter filter) {
+		return IterUtil.filter(collection, filter);
+	}
+
+	/**
+	 * 去除{@code null} 元素,此方法直接修改原集合
+	 * 
+	 * @param  集合类型
+	 * @param  集合元素类型
 	 * @param collection 集合
 	 * @return 处理后的集合
 	 * @since 3.2.2
 	 */
-	public static  Collection removeEmpty(Collection collection) {
-		return filter(collection, new Filter() {
+	public static , E> T removeNull(T collection) {
+		return filter(collection, new Filter() {
 			@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  Collection removeBlank(Collection collection) {
-		return filter(collection, new Filter() {
+	public static , E extends CharSequence> T removeEmpty(T collection) {
+		return filter(collection, new Filter() {
 			@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 , E extends CharSequence> T removeBlank(T collection) {
+		return filter(collection, new Filter() {
+			@Override
+			public boolean accept(E e) {
+				return StrUtil.isNotBlank(e);
 			}
 		});
 	}
diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/IterUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/IterUtil.java
index 5689a7929..eddd7b62e 100644
--- a/hutool-core/src/main/java/cn/hutool/core/collection/IterUtil.java
+++ b/hutool-core/src/main/java/cn/hutool/core/collection/IterUtil.java
@@ -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  List toList(Iterable iter) {
+		if(null == iter) {
+			return null;
+		}
 		return toList(iter.iterator());
 	}
 
@@ -523,10 +527,10 @@ public class IterUtil {
 	 * @return 第一个元素
 	 */
 	public static  T getFirst(Iterable 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;
 	}
+	
+	/**
+	 * 过滤集合,此方法在原集合上直接修改
+ * 通过实现Filter接口,完成元素的过滤,这个Filter实现可以实现以下功能: + * + *
+	 * 1、过滤出需要的对象,{@link Filter#accept(Object)}方法返回false的对象将被使用{@link Iterator#remove()}方法移除
+	 * 
+ * + * @param 集合类型 + * @param 集合元素类型 + * @param iter 集合 + * @param filter 过滤器接口 + * @return 编辑后的集合 + * @since 4.6.5 + */ + public static , E> T filter(T iter, Filter filter) { + if(null == iter) { + return null; + } + + filter(iter.iterator(), filter); + + return iter; + } + + /** + * 过滤集合,此方法在原集合上直接修改
+ * 通过实现Filter接口,完成元素的过滤,这个Filter实现可以实现以下功能: + * + *
+	 * 1、过滤出需要的对象,{@link Filter#accept(Object)}方法返回false的对象将被使用{@link Iterator#remove()}方法移除
+	 * 
+ * + * @param 集合元素类型 + * @param iter 集合 + * @param filter 过滤器接口 + * @return 编辑后的集合 + * @since 4.6.5 + */ + public static Iterator filter(Iterator iter, Filter filter) { + if (null == iter || null == filter) { + return iter; + } + + while(iter.hasNext()) { + if(false == filter.accept(iter.next())) { + iter.remove(); + } + } + return iter; + } } diff --git a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java index 89af15edc..cf3e382c1 100644 --- a/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/map/MapUtil.java @@ -914,4 +914,29 @@ public class MapUtil { } return map; } + + /** + * 去除Map中值为{@code null}的键值对
+ * 注意:此方法在传入的Map上直接修改。 + * + * @param map Map + * @return map + * @since 4.6.5 + */ + public static Map removeNullValue(Map map) { + if (isEmpty(map)) { + return map; + } + + final Iterator> iter = map.entrySet().iterator(); + Entry entry; + while(iter.hasNext()) { + entry = iter.next(); + if(null == entry.getValue()) { + iter.remove(); + } + } + + return map; + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 8df5e8a62..a170ad4c2 100644 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -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 list = CollUtil.newArrayList("a", "b", "c"); + + ArrayList filtered = CollUtil.filter(list, new Filter() { + + @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 list = CollUtil.newArrayList("a", "b", "c", null, "", " "); + + ArrayList filtered = CollUtil.removeNull(list); + + // 原地过滤 + Assert.assertTrue(list == filtered); + Assert.assertEquals(CollUtil.newArrayList("a", "b", "c", "", " "), filtered); + } + + @Test + public void removeEmptyTest() { + ArrayList list = CollUtil.newArrayList("a", "b", "c", null, "", " "); + + ArrayList filtered = CollUtil.removeEmpty(list); + + // 原地过滤 + Assert.assertTrue(list == filtered); + Assert.assertEquals(CollUtil.newArrayList("a", "b", "c", " "), filtered); + } + + @Test + public void removeBlankTest() { + ArrayList list = CollUtil.newArrayList("a", "b", "c", null, "", " "); + + ArrayList filtered = CollUtil.removeBlank(list); + + // 原地过滤 + Assert.assertTrue(list == filtered); + Assert.assertEquals(CollUtil.newArrayList("a", "b", "c"), filtered); + } + @Test public void groupTest() { List list = CollUtil.newArrayList("1", "2", "3", "4", "5", "6"); @@ -576,11 +627,11 @@ public class CollUtilTest { public void zipTest() { Collection keys = CollUtil.newArrayList("a", "b", "c", "d"); Collection values = CollUtil.newArrayList(1, 2, 3, 4); - + Map 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());