Java8中的Collectors.toMap

Collectors.toMap会经常和流stream配合使用,可以将一个List转化为Map。在使用的过程中需要避免key冲突问题,通过以下例子就一目了然了。

  • 例子
package com.company;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class collectorsToMapTest {
    public static class Person {
        private String name;
        private Integer age;

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }
    }

    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("张三", 18));
        list.add(new Person("李四", 20));

        Map<Integer, Person> collect1 = list.stream().collect(Collectors.toMap(Person::getAge, Function.identity()));
        System.out.println(collect1);
        System.out.println(collect1.get(18).getName() + "\n" + "--------------");

        // 解决key相同问题 (oldValue, newValue) -> oldValue
        list.add(new Person("王五", 20));
        Map<Integer, Person> collect2 = list.stream().collect(Collectors.toMap(Person::getAge, Function.identity(), (a, b) -> a));
        System.out.println(collect2);
        System.out.println(collect2.get(20).getName() + "\n" + "--------------");

        // 使用参数 v -> v 等价于 Function.identity()
        list.remove(list.size()-1);
        list.add(new Person("王五", 20));
        Map<Integer, Person> collect3 = list.stream().collect(Collectors.toMap(Person::getAge, v -> v, (a, b) -> b));
        System.out.println(collect3);
        System.out.println(collect3.get(20).getName() + "\n" + "--------------");

        // 获取keys和values
        Set<Integer> keys = collect3.keySet();
        Collection<Person> values = collect3.values();
        keys.stream().forEach(System.out::println);
        values.stream().map(t -> t.getName()).forEach(System.out::println);
    }
}
  • 输出
{18=com.company.collectorsToMapTest$Person@7cc355be, 20=com.company.collectorsToMapTest$Person@6e8cf4c6}
张三
--------------
{18=com.company.collectorsToMapTest$Person@7cc355be, 20=com.company.collectorsToMapTest$Person@6e8cf4c6}
李四
--------------
{18=com.company.collectorsToMapTest$Person@7cc355be, 20=com.company.collectorsToMapTest$Person@7530d0a}
王五
--------------
18
20
张三
王五
  • 参考

(1)https://blog.csdn.net/qq_38826019/article/details/109401567
(2)https://blog.csdn.net/weixin_34138255/article/details/93182204?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control
(3)https://blog.csdn.net/zijikanwa/article/details/103034971?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-6&spm=1001.2101.3001.4242

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容