参考:https://juejin.cn/post/6980135669368619045

转Map

public void toMapTest(){
       List<Person> personList = new ArrayList<>();
       personList.add(new Person("Tom",7000,25,"male","安徽"));
       personList.add(new Person("Jack",8000,30,"female","北京"));
       personList.add(new Person("Lucy",9000,40,"male","上海"));
       personList.add(new Person("Airs",10000,40,"female","深圳"));
       //(k1,k2) -> k1表示如果存在相同的key,将第一个匹配的元素作为内容,第二个舍弃!
       Map<Integer, Person> collect = personList.stream().collect(Collectors.toMap(Person::getAge, v -> v, (k1, k2) -> k1));
       System.out.println(collect);
   }

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

Maven Previous
IDEA常用快捷键 Next