Issue
I'm creating an utility class replacing generic types with declared ones. Currently using Apache Netbeans 12.0 as my IDE, it won't assume those types at compile time. I don't know if this is a Java conceptual problem or if it is Netbeans'.
Here's my code so far:
protected class BoardMap<Point, Cell> extends AbstractMap<Point, Cell>{
@Override
public Set<Map.Entry<Point, Cell>> entrySet() {
Set<Map.Entry<Point, Cell>> mapSet = new HashSet<>();
this.keySet().forEach(key -> {
mapSet.add(new AbstractMap.SimpleEntry<>(key, this.get(key)));
});
return mapSet;
}
/**]
*
* @param key Point as the Cell key positioning
* @return Returns the cell represented by key
*/
public Cell getCell(Point key) {
Iterator<Entry<Point, Cell>> i = entrySet().iterator();
if (key==null) {
while (i.hasNext()) {
Entry<Point, Cell> e = i.next();
if (e.getKey()==null)
return (Cell) e.getValue();
}
} else {
while (i.hasNext()) {
Entry<Point, Cell> e = i.next();
if (key.equals(e.getKey()))
return (Cell) e.getValue();
}
}
return null;
}
}
These two classes, Point and Cell (K and V, respectively) should replace all generic types when extending class, at least I thought so.
But in my main code, when using BoardMap.get(), it says its returning type is Object, not Cell as it should be.
On the image above, c.getPerimeter().add() accepts an instance of Cell, but it says boardMap.getCell() is returning Object not Cell, as BoardMap class clearly states it should.
Is this IDE's problem or am I forgetting something?
Solution
Change
protected class BoardMap<Point, Cell> extends AbstractMap<Point, Cell>{
to
protected class BoardMap extends AbstractMap<Point, Cell>{
since BoardMap
is not generic. You have delcared 2 generic parameters Point
and Cell
this way and later on you are using raw types (not declaring those generic types) causing everything to become object at compile time.
Answered By - Antoniossss