Skip to main content

HashMaps inside the interface

Have you ever needed to have HashMap as constant values in the interface, but couldn't do because you do not know how to do it. Here are the two ways I got.

First: Define class that returns unmodifiable map.


class ConstMaps
{
static Map maps = new HashMap();
static
{
maps.put("mobile", getMobileMap());
maps.put("car", getCarMap());
}
private static Map getMobileMap()
{
Map hm = new HashMap();
hm.put("NOKIA", "Nokia 6121 Classic");
hm.put("SONY", "Sony Ericsson TM506");
hm.put("MOTOROLA", "Motorola Aura");
hm.put("IPHONE", "Apple iPhone 3G");
return hm;
}
private static Map getCarMap()
{
Map hm = new HashMap();
hm.put("FORD", "Ford Capri");
hm.put("VOLKSWAGEN", "VW Scirocco");
hm.put("TOYOTA", "Toyota SpaceCruiser");
hm.put("BMW", "BMW 116i");
hm.put("VAUXHALL", "Vauxhall Frontera");
return hm;
}
public static Map getMap(String mapName)
{
return Collections.unmodifiableMap(maps.get(mapName.toLowerCase()));
}
}


public interface Const_1
{
float LEASTPRICE = 1000;
float MAXPRICE = 100000;

Map MOBILEMAP = MyMapGetter.getMap("mobile");
Map CARMAP = MyMapGetter.getMap("car");
}


Second: Define it using anonymous classes and initializer as shown below.
With this syntax, an anonymous extends the HashMap and uses 'put' in the initialization block.


public interface Const_2
{
float LEASTPRICE = 1000;
float MAXPRICEZ = 100000;

Map MOBILEMAP = new HashMap()
{
{
put("NOKIA", "Nokia 6121 Classic");
put("SONY", "Sony Ericsson TM506");
put("MOTOROLA", "Motorola Aura");
put("IPHONE", "Apple iPhone 3G");
}
};

Map CARMAP = new HashMap()
{
{
put("FORD", "Ford Capri");
put("VOLKSWAGEN", "VW Scirocco");
put("TOYOTA", "Toyota SpaceCruiser");
put("BMW", "BMW 116i");
put("VAUXHALL", "Vauxhall Frontera");
}
};
}


I hope this is useful.

Kovil Pillai P.

Comments

Popular posts from this blog

My Book Shelf - Year 2017

I find it difficult to get this reading order. And I guess it would be still harder to read them without changing the order. I may allow one or two new books to be included in this list, if required. Share book reviews and ratings with Kovil Pillai, and even join a book club on Goodreads.

The Power Game

 Even if you count from the Homo sapiens time, it has been half a million years and from the caveman life to the one who attempts to control the universe, the progress is tremendous. A number of struggles that we have overcome are unimaginable. I am still not convinced whether all these are part of the Divine plan or the Nature adjusts (if at all something is required) itself to any change that takes place in it. As long as we believe in science, we can truly appreciate our power and the things we have achieved. The oldest of the power struggles can be the one between men and women. It is perhaps so subtle that we can’t even call it a power struggle. While we manage to fight against external things, this is something happening in our own race, and we haven’t had an answer yet. A coffee selling 10 times costlier than the price of it in a decade ago may mean the coffee price has been raised. But when you compare it with the price of everything, it is relatively the same. If you look ...