java流中的自定义收集器引发错误

java流中的自定义收集器引发错误,java,collectors,Java,Collectors,我正在尝试用java流编写一个自定义收集器 我有一个employee类,它有id、name和salary。每个属性都有自己的getter和setter方法。 我创建了一个arraylist,其中添加了几个employee对象实例。 然后,我从arraylist创建一个流,并尝试在hashmap中收集流对象。 注意:组合器语法正确,但没有意义 编译器一直说参数不正确。我不知道我犯了什么错误。谁能帮我一下吗 员工类别的代码: 创建arraylist和自定义收集器的代码: 公共类列表测试{ 公共静态v

我正在尝试用java流编写一个自定义收集器

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

编译器一直说参数不正确。我不知道我犯了什么错误。谁能帮我一下吗

员工类别的代码: 创建arraylist和自定义收集器的代码:
公共类列表测试{
公共静态void main(字符串[]args){
员工e1=新员工(100,“R1”,2000);
员工e2=新员工(200,“R2”,4000);
员工e3=新员工(300,“R3”,6000);
员工e4=新员工(400,“R4”,7000);
ArrayList al=新的ArrayList();
al.添加(e1);
加上(e2);
新增(e3);
al.添加(e4);
al.stream().collect(Collector.of(
新建HashMap(),
(HashMap a,员工b)->{
a、 put(b.getId(),b);
返回a;
},
(HashMap c,HashMap d)->c,
Function.identity())
);
}
}
我得到的错误是: 线程“main”java.lang中出现异常。错误:未解决的编译问题: 类型收集器中的(Supplier、BiConsumer、BinaryOperator、Collector.Characteristics…)方法不适用于参数(HashMap,(HashMap a、Employee b)->{},(HashMap c、HashMap d)->{},函数) 为lambda表达式的参数a指定的类型不兼容 为lambda表达式的参数b指定了不兼容的类型 Void方法不能返回值 为lambda表达式的参数c指定了不兼容的类型 为lambda表达式的参数d指定了不兼容的类型 类型不匹配:无法从HashMap转换为R 类型不匹配:无法从函数转换为收集器。特征
有两个问题,您的第一个参数不是有效的
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()
));

这将产生一个
Map
对象,该对象将ID映射到
Employee
本身

谢谢你,马克!!这很好用。事实上,我正在练习编写一个自定义收集器,并尝试了这个。对不起,这是一个意外的点击。我不是有意的。“我现在改了。”霍尔格给了我一个很好的建议。我修改了答案,以防有人决定使用这个答案的组合器作为起点。
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
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()
));
al.stream().collect(Collectors.toMap(Employee::getId, Function.identity()));