java - Creating a custom multi-key HashMap -
i have been trying create custom hashmap
contains 2 keys. user creates object of class 2 parameters k
, v
. class made of 2 hashmaps
: natively extended <integer, v>
, key storage <k, integer>
. basically, there 2 keys each value: k
, integer
.
when user wants item, can retrieve both using k
key or integer
key. so, example, if user uses k
system looks in keytable
integer such key , uses integer
find value v
public class keymap<k, v> extends concurrenthashmap<integer, v> { private hashmap<k, integer> keytable = new hashmap<k, integer>(10); @override public v get(object key) { if (key instanceof v) { return super.get(keytable.get(key)); } return super.get(key); } }
however, have never done parameterization before therefore cannot understand what's problem - getting cannot perform instanceof check against type parameter v. use instead erasure object instead since further generic type information erased @ runtime
on line if (key instanceof v) {
.
is possible achieve system want?
you can't use parametrized types (k , v) in operation performed @ runtime (e.g instanceof
operator). in java parametrization information used @ compile-time , removed compiled code. removal referred type erasure. 1
i suggest don't rely on type of key determine hashmap in , instead see if key valid key first hashmap, , if not, try second. keep in mind k of type integer, meaning both key types identical.
Comments
Post a Comment