问题
I have a HashMap defined:
HashMap<String,ArrayList<Thing>> hashmap = new HashMap<>();
I do need an ArrayList since I want one key to have multiple values. This was the best solution I found for that problem. The Thing class is a parent class for a bunch of other classes. Problem is when I try to add I get an error.
hashmap.put(b, world.ports);
This is the error:
no suitable method found for put(String,ArrayList<SeaPort>)
method Map.put(String,ArrayList<Thing>) is not applicable
(argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)
method AbstractMap.put(String,ArrayList<Thing>) is not applicable
(argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)
method HashMap.put(String,ArrayList<Thing>) is not applicable
(argument mismatch; ArrayList<SeaPort> cannot be converted to ArrayList<Thing>)
` I dont understand this since SeaPort extends Thing isn't it supposed to be compatible? I have read a bunch of upcasting and downcasting threads but I don't see how they apply here. Here is the Thing class:
package seaportprogram;
import java.util.Scanner;
public class Thing implements Comparable<Thing>{
String name;
int index;
int parent;
World world;
public Thing(){
name = null;
index = 0;
parent =0;
}
//Thing Scanner Constructor
public Thing (Scanner sc){
if(sc != null){
name = sc.next();
index = sc.nextInt();
parent = sc.nextInt();
}
}
// Get Index
public int getIndex(){
return index;
}
//Get Name
public String getName(){
return name;
}
//Get Parent
public int getParent(){
return parent;
}
//Thing To String
public String toString(){
return name + " " + index;
}
//Auto-Gen Compare Method
@Override
public int compareTo(Thing o) {
// TODO Auto-generated method stub
return 0;
}
}//End - Class Thing
Here is the SeaPort class:
package seaportprogram;
import java.util.ArrayList;
import java.util.Scanner;
class SeaPort extends Thing {
ArrayList<Dock> docks;
ArrayList<Ship> que;
ArrayList<Ship> ships;
ArrayList<Person> persons;
//Sea Port Scanner Constructor
public SeaPort(Scanner sc){
super(sc);
docks = new ArrayList<>();
que = new ArrayList<>();
ships = new ArrayList<>();
persons = new ArrayList<>();
}
//Set Docks
public void setDocks(ArrayList<Dock> docks){
this.docks = docks;
}
//Get Docks
public ArrayList<Dock> getDocks(){
return docks;
}
//Set Ships
public void setShips(ArrayList<Ship> ships){
this.ships = ships;
}
//Get Ships
public ArrayList<Ship> getShips(){
return ships;
}
//Set Que
public void setQue(ArrayList<Ship> que){
this.que = que;
}
//Get Que
public ArrayList<Ship> getQue(){
return que;
}
//Sea Port To String
public String toString(){
String string = "\n\n Sea Port: " + super.toString() + "\n";
for(Dock md: docks){
string += "\n" + md + "\n";
}
string += "\n\n --- List of all ships in Que: ";
for(Ship ms: que){
string += "\n > " + ms;
}
string += "\n\n --- List of all Ships:";
for(Ship ms: ships){
string += "\n > " + ms;
}
string += "\n\n --- List of all Persons:";
for(Person mp: persons){
string += "\n > " + mp;
}
return string;
}//End
}// End Sea Port Class
This is World class:
package seaportprogram;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class World extends Thing{
ArrayList<SeaPort> ports;
PortTime time;
//World Scanner Constructor
public World(Scanner sc){
super (sc);
ports = new ArrayList<>();
}
//Set Ports
}
Any way to make this happen? TIA! update: I am trying to print the map and I get an null pointer exception. I think this is because the list is being initialized both in seaport and in the MyHashMap class. I cant seem to find a way to get around this and I'm not even sure this is the reason for the error. Here is the toString():
public String toString(){
if(map.isEmpty())
{
System.out.println("The hashMap is empty");
return "empty";
}
String display=" ";
Iterator<String> itr = map.keySet().iterator();
while (itr.hasNext()) {
display =display + itr.next();
Iterator<ArrayList<T>> itr2 = map.values().iterator();
while (itr2.hasNext()) {
display +=itr2.next();
}
}
return display;
}
and this is the call to it from the gui:
jta.setText(map.toString());
回答1:
ArrayList<Derived_Class>
does not extend ArrayList<Base_Class>
.
If you want to use an HashMap like this, one solution could be to write your own wrapper class using templates:
import java.util.ArrayList;
import java.util.HashMap;
public class MyHashMap<T extends Thing> {
private HashMap<String, ArrayList<T>> map;
public MyHashMap() {
map = new HashMap<>();
}
public void add(String s, T element) {
ArrayList<T> list = null;
if ((list = map.get(s)) == null)
map.put(s, list = new ArrayList<T>());
list.add(element);
}
public ArrayList<T> get(String s) {
return map.get(s);
}
}
if you want an HashMap of SeaPorts, you can create a new HashMap like this,
MyHashMap<SeaPort> map = new MyHashMap<SeaPort>();
and use custom setters and getters to access the arraylists
回答2:
Seaport
may extend Thing
, but ArrayList<SeaPort>
does not extend ArrayList<Thing>
.
If you just want to store a map of Strings to Things, you don't need ArrayList. If you really want to map Strings to Lists of Things, then you can't do it like this.
来源:https://stackoverflow.com/questions/49736035/getting-incompatible-error-for-parent-child-classes-java