!771 【6.x】优化支持一键多值的Map集合扩展,为其添加统一接口

Merge pull request !771 from Createsequence/feat-multi-map
This commit is contained in:
Looly
2022-08-30 16:27:53 +00:00
committed by Gitee
10 changed files with 941 additions and 236 deletions

View File

@@ -0,0 +1,160 @@
package cn.hutool.core.map;
import cn.hutool.core.map.multi.CollectionValueMap;
import cn.hutool.core.map.multi.MultiValueMap;
import cn.hutool.core.text.StrUtil;
import org.junit.Assert;
import org.junit.Test;
import java.util.*;
public class CollectionValueMapTest {
@Test
public void putTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
Assert.assertNull(map.put(1, Arrays.asList("a", "b")));
Collection<String> collection = map.put(1, Arrays.asList("c", "d"));
Assert.assertEquals(Arrays.asList("a", "b"), collection);
}
@Test
public void putAllTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
Map<Integer, Collection<String>> source = new HashMap<>();
source.put(1, Arrays.asList("a", "b", "c"));
map.putAll(source);
Assert.assertEquals(1, map.size());
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
}
@Test
public void putValueTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
Assert.assertTrue(map.putValue(1, "a"));
Assert.assertTrue(map.putValue(1, "b"));
Assert.assertTrue(map.putValue(1, "c"));
Assert.assertEquals(1, map.size());
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
}
@Test
public void putAllValueTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
Assert.assertTrue(map.putAllValues(1, Arrays.asList("a", "b", "c")));
Assert.assertEquals(1, map.size());
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
Map<Integer, Collection<String>> source = new HashMap<>();
Assert.assertTrue(map.putValue(1, "e"));
Assert.assertTrue(map.putValue(1, "f"));
map.putAllValues(source);
Assert.assertEquals(Arrays.asList("a", "b", "c", "e", "f"), map.get(1));
}
@Test
public void putValuesTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
}
@Test
public void testFilterAllValues() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
Assert.assertEquals(map, map.filterAllValues((k, v) -> StrUtil.equals(v, "a")));
Assert.assertEquals(Collections.singletonList("a"), map.getValues(1));
Assert.assertEquals(Collections.singletonList("a"), map.getValues(2));
Assert.assertEquals(map, map.filterAllValues(v -> !StrUtil.equals(v, "a")));
Assert.assertEquals(Collections.emptyList(), map.getValues(1));
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
}
@Test
public void testReplaceAllValues() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
Assert.assertEquals(map, map.replaceAllValues((k, v) -> v + "2"));
Assert.assertEquals(Arrays.asList("a2", "b2", "c2"), map.getValues(1));
Assert.assertEquals(Arrays.asList("a2", "b2", "c2"), map.getValues(2));
Assert.assertEquals(map, map.replaceAllValues(v -> v + "3"));
Assert.assertEquals(Arrays.asList("a23", "b23", "c23"), map.getValues(1));
Assert.assertEquals(Arrays.asList("a23", "b23", "c23"), map.getValues(2));
}
@Test
public void removeValueTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeValue(1, "d"));
Assert.assertTrue(map.removeValue(1, "c"));
Assert.assertEquals(Arrays.asList("a", "b"), map.get(1));
}
@Test
public void removeAllValuesTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeAllValues(1, Arrays.asList("e", "f")));
Assert.assertTrue(map.removeAllValues(1, Arrays.asList("b", "c")));
Assert.assertEquals(Collections.singletonList("a"), map.get(1));
}
@Test
public void removeValuesTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeValues(1, "e", "f"));
Assert.assertTrue(map.removeValues(1, "b", "c"));
Assert.assertEquals(Collections.singletonList("a"), map.get(1));
}
@Test
public void getValuesTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.getValues(1));
}
@Test
public void sizeTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertEquals(0, map.size(2));
Assert.assertEquals(3, map.size(1));
}
@Test
public void allForEachTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>();
map.putValues(1, "a", "b", "c");
List<Integer> keys = new ArrayList<>();
List<String> values = new ArrayList<>();
map.allForEach((k, v) -> {
keys.add(k);
values.add(v);
});
Assert.assertEquals(Arrays.asList(1, 1, 1), keys);
Assert.assertEquals(Arrays.asList("a", "b", "c"), values);
}
@Test
public void allValuesTest() {
MultiValueMap<Integer, String> map = new CollectionValueMap<>(new LinkedHashMap<>());
map.putAllValues(1, Arrays.asList("a", "b", "c"));
map.putAllValues(2, Arrays.asList("d", "e"));
Assert.assertEquals(
Arrays.asList("a", "b", "c", "d", "e"),
map.allValues()
);
}
}

View File

@@ -0,0 +1,160 @@
package cn.hutool.core.map;
import cn.hutool.core.map.multi.ListValueMap;
import cn.hutool.core.map.multi.MultiValueMap;
import cn.hutool.core.text.StrUtil;
import org.junit.Assert;
import org.junit.Test;
import java.util.*;
public class ListValueMapTest {
@Test
public void putTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
Assert.assertNull(map.put(1, Arrays.asList("a", "b")));
Collection<String> collection = map.put(1, Arrays.asList("c", "d"));
Assert.assertEquals(Arrays.asList("a", "b"), collection);
}
@Test
public void putAllTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
Map<Integer, Collection<String>> source = new HashMap<>();
source.put(1, Arrays.asList("a", "b", "c"));
map.putAll(source);
Assert.assertEquals(1, map.size());
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
}
@Test
public void putValueTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
Assert.assertTrue(map.putValue(1, "a"));
Assert.assertTrue(map.putValue(1, "b"));
Assert.assertTrue(map.putValue(1, "c"));
Assert.assertEquals(1, map.size());
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
}
@Test
public void putAllValueTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
Assert.assertTrue(map.putAllValues(1, Arrays.asList("a", "b", "c")));
Assert.assertEquals(1, map.size());
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
Map<Integer, Collection<String>> source = new HashMap<>();
Assert.assertTrue(map.putValue(1, "e"));
Assert.assertTrue(map.putValue(1, "f"));
map.putAllValues(source);
Assert.assertEquals(Arrays.asList("a", "b", "c", "e", "f"), map.get(1));
}
@Test
public void putValuesTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
}
@Test
public void removeValueTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeValue(1, "d"));
Assert.assertTrue(map.removeValue(1, "c"));
Assert.assertEquals(Arrays.asList("a", "b"), map.get(1));
}
@Test
public void removeAllValuesTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeAllValues(1, Arrays.asList("e", "f")));
Assert.assertTrue(map.removeAllValues(1, Arrays.asList("b", "c")));
Assert.assertEquals(Collections.singletonList("a"), map.get(1));
}
@Test
public void removeValuesTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeValues(1, "e", "f"));
Assert.assertTrue(map.removeValues(1, "b", "c"));
Assert.assertEquals(Collections.singletonList("a"), map.get(1));
}
@Test
public void testFilterAllValues() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
Assert.assertEquals(map, map.filterAllValues((k, v) -> StrUtil.equals(v, "a")));
Assert.assertEquals(Collections.singletonList("a"), map.getValues(1));
Assert.assertEquals(Collections.singletonList("a"), map.getValues(2));
Assert.assertEquals(map, map.filterAllValues(v -> !StrUtil.equals(v, "a")));
Assert.assertEquals(Collections.emptyList(), map.getValues(1));
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
}
@Test
public void testReplaceAllValues() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
Assert.assertEquals(map, map.replaceAllValues((k, v) -> v + "2"));
Assert.assertEquals(Arrays.asList("a2", "b2", "c2"), map.getValues(1));
Assert.assertEquals(Arrays.asList("a2", "b2", "c2"), map.getValues(2));
Assert.assertEquals(map, map.replaceAllValues(v -> v + "3"));
Assert.assertEquals(Arrays.asList("a23", "b23", "c23"), map.getValues(1));
Assert.assertEquals(Arrays.asList("a23", "b23", "c23"), map.getValues(2));
}
@Test
public void getValuesTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.getValues(1));
}
@Test
public void sizeTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertEquals(0, map.size(2));
Assert.assertEquals(3, map.size(1));
}
@Test
public void allForEachTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
map.putValues(1, "a", "b", "c");
List<Integer> keys = new ArrayList<>();
List<String> values = new ArrayList<>();
map.allForEach((k, v) -> {
keys.add(k);
values.add(v);
});
Assert.assertEquals(Arrays.asList(1, 1, 1), keys);
Assert.assertEquals(Arrays.asList("a", "b", "c"), values);
}
@Test
public void allValuesTest() {
MultiValueMap<Integer, String> map = new ListValueMap<>();
map.putAllValues(1, Arrays.asList("a", "b", "c"));
map.putAllValues(2, Arrays.asList("d", "e"));
Assert.assertEquals(
Arrays.asList("a", "b", "c", "d", "e"),
map.allValues()
);
}
}

View File

@@ -0,0 +1,162 @@
package cn.hutool.core.map;
import cn.hutool.core.map.multi.MultiValueMap;
import cn.hutool.core.map.multi.SetValueMap;
import cn.hutool.core.text.StrUtil;
import org.junit.Assert;
import org.junit.Test;
import java.util.*;
public class SetValueMapTest {
@Test
public void putTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
Assert.assertNull(map.put(1, Arrays.asList("a", "b")));
Collection<String> collection = map.put(1, Arrays.asList("c", "d"));
Assert.assertEquals(Arrays.asList("a", "b"), collection);
}
@Test
public void putAllTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
Map<Integer, Collection<String>> source = new HashMap<>();
source.put(1, Arrays.asList("a", "b", "c"));
map.putAll(source);
Assert.assertEquals(1, map.size());
Assert.assertEquals(Arrays.asList("a", "b", "c"), map.get(1));
}
@Test
public void putValueTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
Assert.assertTrue(map.putValue(1, "a"));
Assert.assertTrue(map.putValue(1, "b"));
Assert.assertTrue(map.putValue(1, "c"));
Assert.assertEquals(1, map.size());
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), map.get(1));
}
@Test
public void putAllValueTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
Assert.assertTrue(map.putAllValues(1, Arrays.asList("a", "b", "c")));
Assert.assertEquals(1, map.size());
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), map.get(1));
Map<Integer, Collection<String>> source = new HashMap<>();
Assert.assertTrue(map.putValue(1, "e"));
Assert.assertFalse(map.putValue(1, "e"));
Assert.assertTrue(map.putValue(1, "f"));
Assert.assertFalse(map.putValue(1, "f"));
map.putAllValues(source);
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c", "e", "f")), map.get(1));
}
@Test
public void putValuesTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), map.get(1));
}
@Test
public void removeValueTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeValue(1, "d"));
Assert.assertTrue(map.removeValue(1, "c"));
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b")), map.get(1));
}
@Test
public void removeAllValuesTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeAllValues(1, Arrays.asList("e", "f")));
Assert.assertTrue(map.removeAllValues(1, Arrays.asList("b", "c")));
Assert.assertEquals(Collections.singleton("a"), map.get(1));
}
@Test
public void removeValuesTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertFalse(map.removeValues(1, "e", "f"));
Assert.assertTrue(map.removeValues(1, "b", "c"));
Assert.assertEquals(Collections.singleton("a"), map.get(1));
}
@Test
public void testFilterAllValues() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
Assert.assertEquals(map, map.filterAllValues((k, v) -> StrUtil.equals(v, "a")));
Assert.assertEquals(Collections.singleton("a"), map.getValues(1));
Assert.assertEquals(Collections.singleton("a"), map.getValues(2));
Assert.assertEquals(map, map.filterAllValues(v -> !StrUtil.equals(v, "a")));
Assert.assertEquals(Collections.emptySet(), map.getValues(1));
Assert.assertEquals(Collections.emptySet(), map.getValues(2));
}
@Test
public void testReplaceAllValues() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
Assert.assertTrue(map.putValues(1, "a", "b", "c"));
Assert.assertTrue(map.putValues(2, "a", "b", "c"));
Assert.assertEquals(map, map.replaceAllValues((k, v) -> v + "2"));
Assert.assertEquals(new HashSet<>(Arrays.asList("a2", "b2", "c2")), map.getValues(1));
Assert.assertEquals(new HashSet<>(Arrays.asList("a2", "b2", "c2")), map.getValues(2));
Assert.assertEquals(map, map.replaceAllValues(v -> v + "3"));
Assert.assertEquals(new HashSet<>(Arrays.asList("a23", "b23", "c23")), map.getValues(1));
Assert.assertEquals(new HashSet<>(Arrays.asList("a23", "b23", "c23")), map.getValues(2));
}
@Test
public void getValuesTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertEquals(Collections.emptyList(), map.getValues(2));
Assert.assertEquals(new HashSet<>(Arrays.asList("a", "b", "c")), map.getValues(1));
}
@Test
public void sizeTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
map.putValues(1, "a", "b", "c");
Assert.assertEquals(0, map.size(2));
Assert.assertEquals(3, map.size(1));
}
@Test
public void allForEachTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
map.putValues(1, "a", "b", "c");
List<Integer> keys = new ArrayList<>();
List<String> values = new ArrayList<>();
map.allForEach((k, v) -> {
keys.add(k);
values.add(v);
});
Assert.assertEquals(Arrays.asList(1, 1, 1), keys);
Assert.assertEquals(Arrays.asList("a", "b", "c"), values);
}
@Test
public void allValuesTest() {
MultiValueMap<Integer, String> map = new SetValueMap<>();
map.putAllValues(1, Arrays.asList("a", "b", "c"));
map.putAllValues(2, Arrays.asList("d", "e"));
Assert.assertEquals(
Arrays.asList("a", "b", "c", "d", "e"),
map.allValues()
);
}
}