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 {    ... } 

  1. in basecodecache, want have java.util.map value can type of basecode i.e. location objects or department objects.
    in locationcache, want java.util.map store location objects.
    in departmentcache, want java.util.map store department objects.

to accomplish this, put code in basecodecache:

private map<integer, basecode> idmap = new hashmap<integer, basecode>(); 

  1. 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); 

  1. i want have method values in java.util.map java.util.list.
    in locationcache method should return list<location>.
    in departmentcache method should return list<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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -