[英]Custom collector throwing error in java stream
I am trying to write a custom collector in java stream. 我试图在Java流中编写一个自定义收集器。
I have a employee class which has id, name and salary. 我有一个具有ID,姓名和薪水的员工班。 Each attribute has its own getter and setter methods.
每个属性都有其自己的getter和setter方法。 I create an arraylist which has several instances of employee objects added in it.
我创建了一个arraylist,其中添加了多个雇员对象实例。 Then I create a stream out of the arraylist and I try to collect the stream objects in a hashmap.
然后,我从arraylist中创建一个流,然后尝试将这些流对象收集在哈希图中。 Note: The combiner is syntactically correct evhentoug it does not make sense.
注意:组合器在语法上是正确的,但是这没有任何意义。
The compiler keeps saying that the arguments are not correct. 编译器一直说这些参数不正确。 I am not sure what is the mistake I am doing.Can any one please help me.
我不确定我在做什么错。任何人都可以帮助我。
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
There are two problems, your first argument is not a valid Supplier
function, it's simply constructing a HashMap at the time of calling the of
method. 有两个问题,第一个参数不是有效的
Supplier
函数,它只是在调用of
方法时构造一个HashMap。 In addition to that, the second argument to the Collector.of
takes a BiConsumer
, so it shouldn't return a value since it has a void return signature. 除此之外,
Collector.of
的第二个参数采用BiConsumer
,因此它不应返回值,因为它具有无效的返回签名。 Try something like this: 尝试这样的事情:
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()
));
I thought I'd also offer a simpler way of accomplishing this without needing to write a custom Collector
: 我以为我也可以提供一种更简单的方法来完成此任务,而无需编写自定义
Collector
:
al.stream().collect(Collectors.toMap(Employee::getId, Function.identity()));
This will result in a Map<Integer, Employee>
object that maps ID to the Employee
itself. 这将导致
Map<Integer, Employee>
对象,该对象将ID映射到Employee
本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.