1、toMap()参数
toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction, Supplier mapSupplier)
第一个参数:用来生成key值的。
第二个参数:用来生成value值的。
第三个参数:可选,用来定义在key值冲突的情况下的value的值。
第四个参数:可选,它返回一个Map
,其中的数据作为键/值填充。
如果我们没有传递 Supplier mapSupplier
,那么默认的supplier
将返回HashMap
。
如果我们想要其他实例,比如LinkedHashMap
,我们需要将supplier
传递为LinkedHashMap::new
。
public static void main(String[] args) {
List<Person> list = new ArrayList();
list.add(new Person(1, "1"));
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