This commit is contained in:
Looly 2021-06-15 10:28:59 +08:00
parent 395942298e
commit 56703c5a55
3 changed files with 26 additions and 15 deletions

View File

@ -1554,10 +1554,10 @@ public class CollUtil {
* @param map Map * @param map Map
* @param editor 编辑器接口 * @param editor 编辑器接口
* @return 过滤后的Map * @return 过滤后的Map
* @see MapUtil#filter(Map, Editor) * @see MapUtil#edit(Map, Editor)
*/ */
public static <K, V> Map<K, V> filter(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) {
return MapUtil.filter(map, editor); return MapUtil.edit(map, editor);
} }
/** /**
@ -1830,7 +1830,7 @@ public class CollUtil {
* @since 3.0.4 * @since 3.0.4
*/ */
public static Map<String, String> zip(String keys, String values, String delimiter, boolean isOrder) { public static Map<String, String> zip(String keys, String values, String delimiter, boolean isOrder) {
return ArrayUtil.zip(StrUtil.split(keys, delimiter), StrUtil.split(values, delimiter), isOrder); return ArrayUtil.zip(StrUtil.splitToArray(keys, delimiter), StrUtil.splitToArray(values, delimiter), isOrder);
} }
/** /**

View File

@ -623,7 +623,7 @@ public class MapUtil {
* @param editor 编辑器接口 * @param editor 编辑器接口
* @return 过滤后的Map * @return 过滤后的Map
*/ */
public static <K, V> Map<K, V> filter(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;
} }
@ -716,7 +716,7 @@ public class MapUtil {
* @since 3.2.2 * @since 3.2.2
*/ */
public static <T> Map<T, T> reverse(Map<T, T> map) { public static <T> Map<T, T> reverse(Map<T, T> map) {
return filter(map, (Editor<Entry<T, T>>) t -> new Entry<T, T>() { return edit(map, t -> new Entry<T, T>() {
@Override @Override
public T getKey() { public T getKey() {
@ -905,7 +905,7 @@ public class MapUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <K, V> Map<K, V> getAny(Map<K, V> map, final K... keys) { public static <K, V> Map<K, V> getAny(Map<K, V> map, final K... keys) {
return filter(map, (Filter<Entry<K, V>>) entry -> ArrayUtil.contains(keys, entry.getKey())); return filter(map, entry -> ArrayUtil.contains(keys, entry.getKey()));
} }
/** /**

View File

@ -1,18 +1,15 @@
package cn.hutool.core.map; package cn.hutool.core.map;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Editor;
import cn.hutool.core.lang.Filter;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
public class MapUtilTest { public class MapUtilTest {
@Test @Test
public void filterTest() { public void filterTest() {
Map<String, String> map = MapUtil.newHashMap(); Map<String, String> map = MapUtil.newHashMap();
@ -21,7 +18,7 @@ public class MapUtilTest {
map.put("c", "3"); map.put("c", "3");
map.put("d", "4"); map.put("d", "4");
Map<String, String> map2 = MapUtil.filter(map, (Filter<Entry<String, String>>) t -> Convert.toInt(t.getValue()) % 2 == 0); Map<String, String> map2 = MapUtil.filter(map, t -> Convert.toInt(t.getValue()) % 2 == 0);
Assert.assertEquals(2, map2.size()); Assert.assertEquals(2, map2.size());
@ -30,14 +27,28 @@ public class MapUtilTest {
} }
@Test @Test
public void filterForEditorTest() { public void filterContainsTest() {
Map<String, String> map = MapUtil.newHashMap();
map.put("abc", "1");
map.put("bcd", "2");
map.put("def", "3");
map.put("fgh", "4");
Map<String, String> map2 = MapUtil.filter(map, t -> StrUtil.contains(t.getKey(), "bc"));
Assert.assertEquals(2, map2.size());
Assert.assertEquals("1", map2.get("abc"));
Assert.assertEquals("2", map2.get("bcd"));
}
@Test
public void editTest() {
Map<String, String> map = MapUtil.newHashMap(); Map<String, String> map = MapUtil.newHashMap();
map.put("a", "1"); map.put("a", "1");
map.put("b", "2"); map.put("b", "2");
map.put("c", "3"); map.put("c", "3");
map.put("d", "4"); map.put("d", "4");
Map<String, String> map2 = MapUtil.filter(map, (Editor<Entry<String, String>>) t -> { Map<String, String> map2 = MapUtil.edit(map, t -> {
// 修改每个值使之*10 // 修改每个值使之*10
t.setValue(t.getValue() + "0"); t.setValue(t.getValue() + "0");
return t; return t;
@ -74,7 +85,7 @@ public class MapUtilTest {
map.put("b", "2"); map.put("b", "2");
map.put("c", "3"); map.put("c", "3");
map.put("d", "4"); map.put("d", "4");
Object[][] objectArray = MapUtil.toObjectArray(map); Object[][] objectArray = MapUtil.toObjectArray(map);
Assert.assertEquals("a", objectArray[0][0]); Assert.assertEquals("a", objectArray[0][0]);
Assert.assertEquals("1", objectArray[0][1]); Assert.assertEquals("1", objectArray[0][1]);