Reputation: 5
I am iterating through a map whose keys are charts and values are data sets which will be displayed on charts. Data sets are lists of maps because I have multiple XYSeries displayed on each of my Charts (one series - one map with x and y values). In some charts x-axis/y-axis values are Doubles, and in others Integers. Thus, my data sets are type < ? extends Number>. What am I doing wrong?
for (Map.Entry<Chart, List<Map<? extends Number, ? extends Number>>> entry : tcInstance.getMapChartDataset().entrySet()) {
switch (entry.getKey().getTitle()) {
case something:
entry.setValue(listOfMaps1);
break;
case something else:
entry.setValue(listOfMaps2);
break;
// other case options
}
}
These are the declarations of lists of maps:
static List<Map<Integer, Double>> listOfMaps1 = new ArrayList<>();
static List<Map<Double, Double>> listOfMaps2 = new ArrayList<>();
I expected values to be set, but instead I got these errors which tell that the method setValue is not applicable for the arguments (List>) (and the same error for the arguments (List>).
Upvotes: 0
Views: 76
Reputation: 140318
A List<Map<Integer,Double>>
is not a List<Map<? extends Number,? extends Number>>
.
If it were, I could do this:
List<Map<Integer,Double>> list = new ArrayList<>();
List<Map<? extends Number,? extends Number>> listX = list; // Doesn't work, pretend it does.
Map<Double,Integer> map = new HashMap<>();
map.put(0.0, 0);
listX.add(map);
for (Map<Integer, Double> e : list) {
Integer i = e.keySet().iterator().next(); // ClassCastException!
}
You would get a ClassCastException
because e
has a Double
key, not Integer
as expected.
If you add an extra upper bound to the wildcarded list:
List<? extends Map<? extends Number,? extends Number>>
^-------^ here
then you wouldn't have been able to add map
to listX
, so it would be safe.
Upvotes: 3