java 流中的自定义收集器抛出错误

Custom collector throwing error in java stream

我正在尝试在 java 流中编写自定义收集器。

我有一个雇员class,它有身份证、姓名和薪水。每个属性都有自己的 getter 和 setter 方法。 我创建了一个数组列表,其中添加了多个员工对象实例。 然后我从 arraylist 创建一个流,并尝试在 hashmap 中收集流对象。 注意:组合器在语法上是正确的,即使它没有意义。

编译器一直说参数不正确。我不确定我是什么错误 doing.Can 请任何人帮助我。

员工代码class:

public class Employee { 
    private int id;
    private String name;
    private int salary;

    public Employee(int id,String name,int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }   
    public int getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public int getSalary() {
        return salary;
    }   
}

创建数组列表和自定义收集器的代码:

public class ListTest {

    public static void main(String[] args) {

        Employee e1 = new Employee (100,"R1",2000);
        Employee e2 = new Employee (200,"R2",4000);
        Employee e3 = new Employee (300,"R3",6000);
        Employee e4 = new Employee (400,"R4",7000);

        ArrayList<Employee> al = new ArrayList<>();
        al.add(e1);
        al.add(e2);
        al.add(e3);
        al.add(e4);

        al.stream().collect(Collector.of(
            new HashMap<Integer, Employee>(),
            (HashMap<Integer, Employee> a, Employee b) -> {
                a.put(b.getId(), b);
                return a;
            },
            (HashMap<Integer, Employee> c, HashMap<Integer, Employee> d) -> c,
            Function.identity())
        );
    }
}

我得到的错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The method of(Supplier<R>, BiConsumer<R,T>, BinaryOperator<R>, Collector.Characteristics...) in the type Collector is not applicable for the arguments (HashMap<Integer,Employee>, (HashMap<Integer, Employee> a, Employee b) -> {}, (HashMap<Integer, Employee> c, HashMap<Integer, Employee> d) -> {}, Function<Object,Object>)
    Incompatible type specified for lambda expression's parameter a
    Incompatible type specified for lambda expression's parameter b
    Void methods cannot return a value
    Incompatible type specified for lambda expression's parameter c
    Incompatible type specified for lambda expression's parameter d
    Type mismatch: cannot convert from HashMap<Integer,Employee> to R
    Type mismatch: cannot convert from Function<Object,Object> to Collector.Characteristics

有两个问题,你的第一个参数不是有效的Supplier函数,它只是在调用of方法时构造了一个HashMap。除此之外,Collector.of 的第二个参数采用 BiConsumer,因此它不应该 return 值,因为它具有 void return 签名。尝试这样的事情:

Map<Integer, Employee> employees = al.stream().collect(Collector.of(
   () -> new HashMap<>(),
   (HashMap<Integer, Employee> a, Employee b) -> {
       a.put(b.getId(), b);
   },
   (HashMap<Integer, Employee> c, HashMap<Integer, Employee> d) -> {
       c.putAll(d);
       return c;
   },
   Function.identity()
));

我想我还会提供一种更简单的方法来完成此操作,而无需编写自定义 Collector:

al.stream().collect(Collectors.toMap(Employee::getId, Function.identity()));

这将导致 Map<Integer, Employee> 对象将 ID 映射到 Employee 本身。