1

I want to create an implementation of java.util.Map which only takes String as key and value.

I tried several things

abstract class BaseMap<String,String> implements Map<K,V>

this gives a compile error mebe because I am implementing the interface and not fulfilling the full definition of Key and Value

then I tried extending the interface to define types, it does nt let me do that as well

public interface IBaseMap<String, String> extends Map<K, V>

It still gives error "The type parameter String is hiding java.lang.String"

Please help, thanks in advance

7
  • 3
    implements Map<String, String> Commented Sep 11, 2013 at 16:47
  • abstract class BaseMap<String, String> implements Map<String,String> this gives an error too. Now, if I remove the Generics from class name it works, but then dont I loose the type enforcement from Generics? Commented Sep 11, 2013 at 16:48
  • Valid is either BaseMap implements Map<String,String> or BaseMap<K,V> implements Map<K,V>... Commented Sep 11, 2013 at 16:49
  • 2
    No, since the enforcement is coming from the specified types you are implementing. You are nailing the implementation of Map to String, so your BaseMap cannot overcome that (thus is nailed down to String, too) Commented Sep 11, 2013 at 16:50
  • 1
    Feel free to accept one of the other answers. They might have been after me, but they are more precise and explanatory. I suggest anubhava's answer since he answers your question most precisely (imho). Commented Sep 11, 2013 at 17:06

2 Answers 2

9

You need to provide concrete types while extending an Interface of generics like:

abstract class StringToStringMap implements Map<String, String> {
   // ...
}

Or if you want to extend Map<K, v> interface then:

interface IBaseMap extends Map<String, String> {
   // ...       
}
Sign up to request clarification or add additional context in comments.

Comments

1

Do you really need inheritance here? You should really avoid using it like this. You can work with composition in better way:

class CustomMap<K extends String, V extends String> {
    private Map<K, V> map;

    public Map<K, V> getMap() {
        return map;
    }
}

Now, you can instantiate your CustomMap with only String as type argument, which eventually is used as key and value for enclosed Map.


In fact, no need to make the class generic at all, as specified by @TedHopp in comments. Just enclose a Map<String, String>:

class CustomMap {
    private Map<String, String> map;
}

5 Comments

Since String is final, there's no need for K extends String, etc.
@TedHopp. Yeah. But that is necessary to specify bounds for type parameters
I see no need for type parameters at all. Just declare private Map<String, String> map; and public Map<String, String> getMap() . . .
@TedHopp. I guess that is necessary for enforcing type parameters for Map.
@TedHopp. OOPs. Totally missed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.