'Streams using doubles
I want to process animal weights, I have an ArrayList called Animal which holds the different types of weight. For each attribute I have written a ‘Get’ which returns the value (an integer). This works fine for integers. I have now added a double which records the distance between their current weight and the average weight. I have changed getDiffFromAvgWgt routine to return a double and changed the return type of this routine to return a Number[] array (it was a Integer[] array). It won’t compile. Netbeans suggests changing the return type of the routine to double[]. If I do this that line works but none of the integers do! I thought that a Number[] array would hold either because my understanding is that Number is a superset which includes integer and Double. Why doesn’t it? Is there a better way to do it?
public Number[] getValueListByAttribid(int Attribid) {
switch (Attribid) {
case 108:
return creature.stream().map(Animal::getWgt).toArray(i -> new Integer[i]);
case 109:
return creature.stream().mapToDouble(Animal::getDiffFromAvgWgt ).toArray();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
