09. maj 2018 - 15:17
Der er
3 kommentarer
HaspMap
Jeg får følgende fejl på nedenstående kode. Nogen der kan se hvad fejlen er? java.lang.ClassCastException: java.util.HashMap cannot be cast to java.util.TreeMap ---------------------------- protected static Map getResource(String resourceId, String function) { TreeMap functionResources = (TreeMap) ((Map) AbstractParameterManager.getResource(resourceId)).get(function); if (functionResources == null) { functionResources = new TreeMap(); ((Map) AbstractParameterManager.getResource(resourceId)).put(function, functionResources); } return functionResources; }
Annonceindlæg fra Computerworld it-jobbank
Interfacet Map har to implementationer HashMap og TreeMap. Map m<X,Y> = new HashMap<X,Y>(); HashMap m<X,Y> = new HashMap<X,Y>(); Map m<X,Y> = new TreeMap<X,Y>(); TreeMap m<X,Y> = new TreeMap<X,Y>(); virker alle, men: TreeMap m<X,Y> = new HashMap<X,Y>(); HashMap m<X,Y> = new TreeMap<X,Y>(); virker ikke.
Hvis jeg skulle gaette paa din kode: TreeMap functionResources = (TreeMap) ((Map) AbstractParameterManager.getResource(resourceId)).get(function); -> Map functionResources = ((Map) AbstractParameterManager.getResource(resourceId)).get(function); og: functionResources = new TreeMap(); -> functionResources = new HashMap();
Hm, synes ikke jeg kan få det til at fungere. Der kommer llidt mere kode nedenfor: public abstract class AbstractParameterManager implements ParameterManager { protected static final Map resourceLocations = ResourceLocator.locateResources(); protected static final Map resources = new TreeMap(); protected final Map overrides = new TreeMap(); protected static Object getResource(String resourceId) { Map resourceMap = (Map) resources.get(resourceId); if (resourceMap == null) { resourceMap = new TreeMap(); resources.put(resourceId, resourceMap); } return resourceMap; } protected static Map getResource(String resourceId, String function) { TreeMap functionResources = (TreeMap) ((Map) AbstractParameterManager.getResource(resourceId)).get(function); if (functionResources == null) { functionResources = new TreeMap(); ((Map) AbstractParameterManager.getResource(resourceId)).put(function, functionResources); } return functionResources; }
Kurser inden for grundlæggende programmering