!479 新增CollStreamUtil.groupKeyValue(java.util.Collection<E>, java.util.function.Function<E,K>, java.util.function.Function<E,V>),用于分组后能直接获取属性

Merge pull request !479 from 阿超/v5-dev
This commit is contained in:
Looly
2021-12-12 10:58:26 +00:00
committed by Gitee
2 changed files with 60 additions and 1 deletions

View File

@@ -149,6 +149,26 @@ public class CollStreamUtilTest {
}
@Test
public void testGroupKeyValue() {
Map<Long, List<Long>> map = CollStreamUtil.groupKeyValue(null, Student::getTermId, Student::getClassId);
Assert.assertEquals(map, Collections.EMPTY_MAP);
List<Student> list = new ArrayList<>();
map = CollStreamUtil.groupKeyValue(list, Student::getTermId, Student::getClassId);
Assert.assertEquals(map, Collections.EMPTY_MAP);
list.add(new Student(1, 1, 1, "张三"));
list.add(new Student(1, 2, 1, "李四"));
list.add(new Student(2, 2, 1, "王五"));
map = CollStreamUtil.groupKeyValue(list, Student::getTermId, Student::getClassId);
Map<Long, List<Long>> compare = new HashMap<>();
compare.put(1L, Arrays.asList(1L, 2L));
compare.put(2L, Collections.singletonList(2L));
Assert.assertEquals(compare, map);
}
@Test
public void testTranslate2List() {
List<String> list = CollStreamUtil.toList(null, Student::getName);