I have written a TrieMap<V> implements Map<String,V> class which is obviously keyed from Strings. This works fine.
I want to enhance it to be keyed with the more general CharSequence. I believe I have managed to achieve the transform apart from one final issue, I can’t create objects of type K.
The signature must now therefore becomeTrieMap<K extends CharSequence,V> implements Map<K,V>. The problem is, as I am sure you know, a TrieMap does not actually store the original keys. (In fact that is one of its main values, it can therefore often take up much less space than an ordinary map.)
To implement Set<Entry<K, V>> entrySet() I therefore must somehow manufacture objects of type K. Is there any way that can be achieved?
I would define the following interface:
and provide an implementation of the interface to the
TrieMapconstructor. When you need to build a new K instance, you ask the interface to do it.You might use something more apt to your data structure as the parameter to the
buildmethod, CharSequence is only a possibility. IF you want to do something more complex, you could structure it like this:With
Stringimplementations: