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

时间:2018-02-23 01:58:03

标签: java collectors

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

我有一个员工类,其中包含id,name和salary。每个属性都有自己的getter和setter方法。 我创建了一个arraylist,其中添加了几个employee对象实例。 然后我从arraylist创建一个流,我尝试在hashmap中收集流对象。 注意:合并器在语法上是正确的,因为它没有意义。

编译器一直说这些参数不正确。我不知道我在做什么错。可以帮助我。

员工类代码:

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

1 个答案:

答案 0 :(得分:3)

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

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