本文介绍了Java8:在Collectors.toMap(..)中使用Function :: identity会产生参数不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这个简单的人POJO:

  public class Person {
private String id;
私有字符串名称;

public Person(String id,String name){
super();
this.id = id;
this.name = name;
}

public String getId(){
return id;
}

public String getName(){
return name;
}
}

我想领取 Map< String,Person> 其中键是Person的 id



  import java.util.ArrayList; 
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

公共类CollectorMain {
public static void main(String [] args){
List< Person> list = new ArrayList<>();
list.add(新人(312,John));
list.add(new Person(454,Alice));
list.add(新人(712,Bob));

Map< String,Person> map = list.stream()
.collect(
Collectors.toMap(Person :: getId,Function :: identity)// COMPILE ERROR
);
System.out.println(map.size());
}
}

但我收到编译错误:

  CollectorMain.java:[14,41]没有为toMap找到合适的方法(Person :: getId,Function :: identity)
方法java.util.stream.Collectors。< T,K,U> toMap(java.util.function.Function<?super T,?extends K>,java.util.function.Function<?super T,?extends U>)不适用
(不能推断类型变量T,K,U
(参数不匹配;方法参考中不兼容的参数类型))
方法java.util.stream .Collectors。< T,K,U> toMap(java.util.function.Function<?super T,?extends K> ;, java.util.function.Function<?super T,?extends U>,java.util .function.BinaryOperator< U>)不适用
(不能推断类型变量T,K,U
(实际和形式参数列表的长度不同))
方法java .util.stream.Collectors。< T,K,U,M> toMap(java.util.function.Function<?super T,?extends K >,&java.util.function.Function LT ;?超级T,?扩展U>,java.util.function.BinaryOperator< U>,java.util.function.Supplier< M>)不适用
(无法推断类型变量T,K,U,M
(实际和正式参数列表长度不同))






作为一个解决方案,我可以用这个lambda 替换 Function :: identity p - > p 是这样的:

  Map< String,Person> map = list.stream()
.collect(
Collectors.toMap(Person :: getId,p - > p)
);

是否有可能使用 Function :: identity

解决方案

您不应该使用方法参考。你应该使用 Function.identity()



如果你使用方法参考,你基本上是试图传递供应商<功能< T&T;>

  Map< String,Person> map = list.stream()。collect(
Collectors.toMap(Person :: getId,Function.identity())
);


Given this simple Person POJO:

public class Person {
    private String id;
    private String name;

    public Person(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

I would like to collect a Map<String, Person> where the key is the id of the Person.

I have tried to implement it like this:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class CollectorMain {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("312", "John"));
        list.add(new Person("454", "Alice"));
        list.add(new Person("712", "Bob"));

        Map<String, Person> map = list.stream()
               .collect(
                    Collectors.toMap(Person::getId, Function::identity) // COMPILE ERROR
               );
        System.out.println(map.size());
    }
}

But I get a compile error:

CollectorMain.java:[14,41] no suitable method found for toMap(Person::getId,Function::identity)
    method java.util.stream.Collectors.<T,K,U>toMap(java.util.function.Function<? super T,? extends K>,java.util.function.Function<? super T,? extends U>) is not applicable
      (cannot infer type-variable(s) T,K,U
        (argument mismatch; incompatible parameter types in method reference))
    method java.util.stream.Collectors.<T,K,U>toMap(java.util.function.Function<? super T,? extends K>,java.util.function.Function<? super T,? extends U>,java.util.function.BinaryOperator<U>) is not applicable
      (cannot infer type-variable(s) T,K,U
        (actual and formal argument lists differ in length))
    method java.util.stream.Collectors.<T,K,U,M>toMap(java.util.function.Function<? super T,? extends K>,java.util.function.Function<? super T,? extends U>,java.util.function.BinaryOperator<U>,java.util.function.Supplier<M>) is not applicable
      (cannot infer type-variable(s) T,K,U,M
        (actual and formal argument lists differ in length))


As a solution I can replace Function::identity with this lambda p -> p like this:

Map<String, Person> map = list.stream()
    .collect(
        Collectors.toMap(Person::getId, p -> p)
    );

Is there a possibility to use Function::identity ?

解决方案

You should not use a method reference. You should use Function.identity().

If you use a method reference, you're basically trying to pass a Supplier<Function<T, T>>.

Map<String, Person> map = list.stream().collect(
    Collectors.toMap(Person::getId, Function.identity())
);

这篇关于Java8:在Collectors.toMap(..)中使用Function :: identity会产生参数不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:20