How to extends HashMap to allow String, String types
Solution 1
All you need is:
import java.util.LinkedHashMap;
@SuppressWarnings("serial")
public class MyMenu extends LinkedHashMap<String, String> {
public MyMenu(){
this.put("index.jsp", "Home Page");
}
}
Remove <String, String>
from your class name and it will work. You are creating a class which is extending LinkedHashMap<String, String>
so your class is already a Map
which takes String
as a key and String
as a value. If you want to create your own generic class then you have to do like this:
Create a class for e.g.:
public class MyMenu<K, V> { ... }
and then extend that class:
public class SomeMyMenu extends MyMenu<String, String> { ... }
In that case you will have to specify the key and value for your class in order for SomeMyMenu
class use the key and value as String
. You can read more about generics here.
But more efficient way to do what you want is to create some final class and declare the map in it like this:
public static final LinkedHashMap<String, String> MAP = new LinkedHashMap<String, String>() {{
put("index.jsp", "Home Page");
}};
And to get the values from your map use:
SomeClass.MAP.get("Some Key");
Solution 2
If your struggle, as you describe, is just with the declaration, then I would try something like:
class MyCustomizedMap extends LinkedHashMap<String, String> {
//...
}
Solution 3
You could do something like below:
public class MyMap extends LinkedHashMap<String,String>{
Solution 4
Composition is a better solution than inheritance in your case:
class MyProperties implements Map<String, String> {
private Map<String, String> delegatee = new LinkedHashMap<String, String>();
// ... delegate your methods to delegatee
}
Solution 5
As long as you are using JDK 1.5 or higher, you can use generics and create a LinkedHashMap that stores String keys and String values and initialize it as follows:
LinkedHashMap<String, String> myMap = new LinkedHashMap<String, String>()
{
{
put("abc", "xyz");
...
};
}
Wil
Updated on July 05, 2022Comments
-
Wil almost 2 years
I need to create a custom class that extends java.util.LinkedHashMap and takes String as key and String as value and has a no-arg constructor that pre-populates this map with initial values.
I am stuck at the first task - how to extend the LinkedHashMap in such a way that instead of generic arguments it accepts only?
I tried this, (not working)
import java.util.LinkedHashMap; @SuppressWarnings("serial") public class MyMenu<String, String> extends LinkedHashMap<String, String> { public MyMenu(){ this.put("index.jsp", "Home Page"); } }
-
Rohit Jain over 11 yearsYou forgot to complete your last sentence. Anyways, it won't matter, unless you show some code you might have tried.
-
Denys Séguret over 11 yearsExtending LinkedHashMap is almost certainly a bad solution. You can use its parameterized form or use composition instead of inheritance.
-
Wil over 11 yearsI would not have problem with composition but then I would need a getter method to getMap() from my object. I wanted my object to be a HashMap so it would be easier to use from jsp with jstl
-
Martin Serrano over 11 yearsi agree with @dystroy. we use a utility method with varargs to initialize a map with initial values.
-
nicholas.hauschild over 11 yearsYou would not necessarily need a getter to get the
Map
in a composition model. You could implement theMap
interface and just delegate to your...delegate... -
Wil over 11 yearsCan someone maybe post a good link that explains why composition is better than extending HashMap? I don't see a problem with extending it in my particular case - I need to store map of jsp page to the page title so that I can dymanically create navigation menu in JSP
-
Louis Wasserman over 11 yearsWhy not just write a static factory method that creates a
LinkedHashMap
, puts the desired elements into it, and returns a result? No need for a fragile subclass here. -
Wil over 11 years@Louis Wasserman Because I want my class to behave like a bean, so I can use it from jsp page with <jsp:useBean.... it will be easy when my class has no-argument constructor. with wrapper factory class it will probably not be so easy to use it from jsp. I'm just a beginner...
-
-
Wil over 11 yearsThanks. This worked. I still would like to get explanation why I don't need <String, String> after MyMenu? I am learning working with generics in Java and any good explanation is very helpful to me.
-
durron597 over 11 yearsComposition is almost always better than inheritance. +1
-
trashgenerator over 6 yearsBut not "always"