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 create a class that extends 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, you need to do the following:
Create a class, for example:
public class MyMenu<K, V> { ... }
and then continue with this class:
public class SomeMyMenu extends MyMenu<String, String> { ... }
In this case, you will need to specify the key and value for your class so that the SomeMyMenu class uses the key and value as String . You can find out more about generics here .
But a more efficient way to do what you want is to create some kind of final class and declare a map in it as follows:
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");
Paulius matulionis
source share