package com.best.buc.verification.constant;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Getter
@Setter
@AllArgsConstructor
public class Person {
private Integer id;
private String name;
public static void main(String[] args) {
List<Person> list = new ArrayList();
list.add(new Person(1, "1"));
// list.add(new Person(1, "4"));
list.add(new Person(2, "2"));
list.add(new Person(3, "3"));
Map<Integer, Person> collect = list.stream().collect(Collectors.toMap(Person::getId, Function.identity()));
Map<Integer, Person> collect1 = list.stream().collect(Collectors.toMap(Person::getId, Function.identity(), (a,b)->a));
Map<In
Stream流Collectors.toMap用法
最新推荐文章于 2024-09-04 17:43:10 发布
本文介绍了Java Stream流的Collectors.toMap方法,详细解释了如何使用该方法生成Map,包括设置key和value的方法,以及在key冲突时如何处理,通过实例展示了在key冲突时不提供合并策略导致的错误。
摘要由CSDN通过智能技术生成