Java Stream Map和flatmap

编程中多数要对集合进行各种操作,获取Map、Set、List等。
Map

比如一个对象List,获取所有人的名字集合

    @Test
    public void should_can_get_name_map() {
        PersonInfo kaka = new PersonInfo("Kaka", 22);
        PersonInfo hustzw = new PersonInfo("Hustzw", 24);

        List<PersonInfo> personInfos = Lists.newArrayList(kaka, hustzw);

        List<String> nameList = personInfos.stream().map(PersonInfo::getName).collect(Collectors.toList());

        assertThat(nameList).contains("Kaka");
    }

HashMap

构建一个属性和其本身的映射,比如根据人名找到人。

    @Test
    public void should_can_get_name_info_map() {
        PersonInfo kaka = new PersonInfo("Kaka", 22);
        PersonInfo hustzw = new PersonInfo("Hustzw", 24);

        List<PersonInfo> personInfos = Lists.newArrayList(kaka, hustzw);

        // 注意, 这里 key 不能重复,否则报错
        Map<String, PersonInfo> nameInfoMap1 = personInfos.stream().collect(Collectors.toMap(PersonInfo::getName, x -> x));
        Map<String, PersonInfo> nameInfoMap2 = personInfos.stream().collect(Collectors.toMap(PersonInfo::getName, Function.identity()));// 效果一样

        assertThat(nameInfoMap1).containsKeys("Kaka", "Hustzw");
        assertThat(nameInfoMap2).containsKeys("Kaka", "Hustzw");
    }

Set

同样是获取集合

    @Test
    public void should_can_get_set() {
        PersonInfo kaka = new PersonInfo("Kaka", 22);
        PersonInfo hustzw = new PersonInfo("Hustzw", 24);

        List<PersonInfo> personInfos = Lists.newArrayList(kaka, hustzw);

        // 注意, 这里 key 不能重复,否则报错
        Set<PersonInfo> adultsSet = personInfos.stream().filter(x -> x.getAge() >= 18).collect(Collectors.toSet());

        assertThat(adultsSet).contains(kaka, hustzw);
        assertThat(adultsSet).contains(hustzw, kaka);
    }

flatmap

Java8的stream中提供flatmap也是用于构建Map,不过区别的是它会把结果打平。看个例子:

有两个List,想对所有元素按年龄分类。如果利用循环可能需要两层循环。

利用 flatemap后,打平后很容易对其操作。

扫描二维码关注公众号,回复: 5874216 查看本文章

代码:

    @Test
    public void should_can_get_name_info_flat_map() {
        PersonInfo kaka = new PersonInfo("Kaka", 22);
        PersonInfo hustzw = new PersonInfo("Hustzw", 22);
        PersonInfo zhangSan = new PersonInfo("ZhangSan", 14);
        PersonInfo lisi = new PersonInfo("LiSi", 14);

        List<PersonInfo> firstInfos = Lists.newArrayList(kaka, zhangSan, zhangSan);
        List<PersonInfo> secondInfos = Lists.newArrayList(hustzw, lisi);

        List<List<PersonInfo>> complexInfos = Lists.newArrayList(firstInfos, secondInfos);

        // 按年龄分组,不会去重
        Map<Integer, List<PersonInfo>> map = complexInfos.stream().flatMap(l -> l.stream()).collect(Collectors.groupingBy(PersonInfo::getAge));

        // 如果需要去重复
        Map<Integer, List<PersonInfo>> distinctMap = complexInfos.stream().flatMap(l -> l.stream())// 重新生成一个Stream对象取而代之
                .distinct().collect(Collectors.groupingBy(PersonInfo::getAge));

        assertThat(map).containsKey(14);
        assertThat(map.get(14).size()).isEqualTo(3);
        assertThat(distinctMap.get(14).size()).isEqualTo(2);
    }

猜你喜欢

转载自blog.csdn.net/hustzw07/article/details/87075841
今日推荐