Java Cannot implement a function in Map ADT interface that takes Pair class as its generic parameter -
i created map interface has generic function entries().
// return iterable collection of key-value entries in map public arraylist<pair<keytype, valuetype>> entries(); the problem is, when try implement interface error in interface file @ entries() function: bound mismatch: type keytype not valid substitute bounded parameter <keytype extends comparable<keytype>> of type pair<keytype,valuetype>
my implementation of function shown below:
public arraylist<pair<keytype, valuetype>> entries(){ arraylist<pair<keytype, valuetype>> list = new arraylist<pair<keytype, valuetype>>(); preorderlist (root, list); return list; } how can solve problem?
you left generic bound out of interface declaration. since pair requires keys comparable each other (keytype extends comparable<keytype>), map need reiterate bound in keytype declaration in order use pair keytype. may need bound valuetype if pair bounds it.
interface map <keytype extends comparable<keytype>, valuetype> { ... } under generic type erasure, pair's keytype replaced comparable. if not bound map's keytype, replaced object, not assignable comparable without narrowing cast can potentially fail.
Comments
Post a Comment