java - Generics: How to get values of a Map without casting -
i have abstract class named basecode
, 2 concrete classes named location
, department
:
public abstract class basecode { private integer id; ... public integer getid() { return id; } public void setid(integer id) { this.id = id; } ... } public class location extends basecode { ... } public class department extends basecode { ... }
i have abstract class named basecodecache
, 2 concrete classes named locationcache
, departmentcache
. locationcache
, departmentcache
using singleton
.
public abstract class basecodecache { ... } public class locationcache extends basecodecache { ... } public class departmentcache extends basecodecache { ... }
- in
basecodecache
, want havejava.util.map
value can type ofbasecode
i.e.location
objects ordepartment
objects.
inlocationcache
, wantjava.util.map
storelocation
objects.
indepartmentcache
, wantjava.util.map
storedepartment
objects.
to accomplish this, put code in basecodecache
:
private map<integer, basecode> idmap = new hashmap<integer, basecode>();
- i want have method store value in
java.util.map
.
to accomplish this, put code in basecodecache
:
public void add(basecode basecode) { if (basecode != null) { idmap.put(basecode.getid(), basecode); } }
this how use location
:
location location = new location(); ... locationcache.getinstance().add(location);
this how use department
:
department department = new department(); ... departmentcache.getinstance().add(department);
- i want have method values in
java.util.map
java.util.list
.
inlocationcache
method should returnlist<location>
.
indepartmentcache
method should returnlist<department>
.
thats stuck. want create method in basecodecache
when method called via locationcache
returns list<location>
, when same method called via departmentcache
returns list<department>
. is possible?
i put code in basecodecache
:
public list<basecode> getlist() { return new arraylist<basecode>(idmap.values()); }
but above code returns list<basecode>
. when call way:
list<location> alllocations = locationcache.getinstance().getlist();
then java not let compile , give error message:
type mismatch: cannot convert
list<basecode>
list<location>
i can fixed getting list<basecode>
, converting list<location>
looping thats not right.
can done?
implement using generics follows:
public abstract class basecodecache<t extends basecode> { private map<integer, t> idmap = new hashmap<>(); public void add(t basecode) { if (basecode != null) { idmap.put(basecode.getid(), basecode); } } public list<t> getlist() { return new arraylist<>(idmap.values()); } } public class locationcache extends basecodecache<location> {} public class departmentcache extends basecodecache<department> {}
this enable following without compilation errors:
locationcache locationcache = new locationcache(); locationcache.add(new location()); list<location> locations = locationcache.getlist();
even better, will compilation errors if try add or retrieve wrong type of objects:
locationcache.add(new department()); // won't compile list<department> departments = locationcache.getlist(); // won't compile
Comments
Post a Comment