I have a simple class called Employee.
public class Employee<T extends Number> {
private final String id;
private final String name;
private final T salary; //generic type salary
public Employee(String id,String name,T salary){
this.id = id;
this.name = name;
this.salary = salary;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public T getSalary() {
return salary;
}
}
Here using reduction, i am able to add Double type. But i need generic type addition. The values in the salary can be either Double or Integer. So is there any way to use provide any flexibility so that i can add any Sub type of Number.
public static void main(String[] args) {
//creates employee list and three employees in it
List < Employee > employees = new LinkedList < > ();
employees.add(new Employee("E001", "John", 30000.00));
employees.add(new Employee("E002", "Mark", 45000.00));
employees.add(new Employee("E003", "Tony", 55000.00));
employees.stream().map(Employee::getSalary).reduce(0, (a, b) -> {
//only able to add double type values, but i need any sub type of number
return a.doubleValue() + b.doubleValue();
});
}
Please help me.