繁体   English   中英

自定义收集器在Java流中引发错误

[英]Custom collector throwing error in java stream

我试图在Java流中编写一个自定义收集器。

我有一个具有ID,姓名和薪水的员工班。 每个属性都有其自己的getter和setter方法。 我创建了一个arraylist,其中添加了多个雇员对象实例。 然后,我从arraylist中创建一个流,然后尝试将这些流对象收集在哈希图中。 注意:组合器在语法上是正确的,但是这没有任何意义。

编译器一直说这些参数不正确。 我不确定我在做什么错。任何人都可以帮助我。

Employee类的代码:

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;
    }   
}

我在其中创建arraylist和自定义收集器的代码:

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 ,因此它不应返回值,因为它具有无效的返回签名。 尝试这样的事情:

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本身。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM