I have a client class which I cannot change.
List<Integer> list1= Arrays.asList(1,2,3);
System.out.println("Total sum is:"+sum(list1));
printlist(list1);
List<Double> list2=Arrays.asList(1.0,2.0,3.0);
System.out.println("Total sum is:"+sum(list2));
printlist(list2);
I have the business logic here
private static Object sum(List<? extends Number> list) {
double sum = 0;
for (Number i: list) {
sum+=i.doubleValue();
}
return sum;
}
So I want to return 6 for integer and 6.0 for a double. How can I do that? I am thinking to typecast sum as int or double based on type but due to type erasure all the info is lost. Can someone help me?