java - Casting return type from interface -
i making lrucache app. i've got interface. cannot change it. implements methods , don't know how return cache instead of interface type.
in method getitem() instead of returning specific value can return whole map.
public class lrucache<k, v> extends linkedhashmap<k, v> implements cache view{ private static final long serialversionuid = 1l; private int limiter; private lrucache<string, object> lruinst; private lrucache(int size) { super(size, 0.75f, true); this.limiter = size; } @override protected boolean removeeldestentry(map.entry<k, v> eldest) { return size() > limiter; } public static<string,object> lrucache<string,object> newinstance(int size) { return new lrucache<>(size); } @override public cacheitem getitem(int index) { list<string> keylist = new arraylist<string>(lruinst.keyset()); (int = 0; < lruinst.size(); i++) { string key = keylist.get(i); object value = lruinst.get(key); if (i == index) { system.out.println("z indexu "+value); return lruinst; } } return null; }//this not work, return map @override public cacheitem getitem(string key) { string aa = lruinst.get(key).tostring(); system.out.println(aa); return (cacheitem) lruinst.getvalue(); }//this not work public interface cacheview { cacheitem getitem(int index); cacheitem getitem(string key);
Comments
Post a Comment