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

Movie List 2013

List of movies I hear about in year 2013 MovieList2013 S.No. Title Rating Language Link 1 Musa, The Warrior 65 Korean http://www.imdb.com/title/tt0275083/ 2 Endukante... Premanta! 35 Telugu http://en.wikipedia.org/wiki/Endukante..._Premanta! 3 Modern Times 60 English http://www.imdb.com/title/tt0027977/ 4 Scent Of A Woman 70 English http://www.imdb.com/title/tt0105323/ 5 Kozhi Kuvuthu 20 Tamil   6 Seven Samurai 50 English http://www.imdb.com/title/tt0047478/ 7 Aalayamani 60 Tamil http://en.wikipedia.org/wiki/Aalayamani 8 Unnale Unnale 35 Tamil http://en.wikipedia.org/wiki/Unnale_Unnale 9 Alex Pandiyan 25 Tamil http://en.wikipedia.org/wiki/Alex_Pandian 10 Payyans 30 Malayalam http://www.nowrunning.com/movie/7709/malayalam/payyans/index.htm 11 Nadodigal 35 Tamil   12 Searching for Sugar Man  60 English http://www.imdb.com/title/tt2125608/ 13 Hitlist 15 Malayalam http://www.nowrunning.com/movie/109...

Stop & Listen

Source: Email forward A man sat at a metro station in Washington DC and started to play the violin; it was a cold January morning. He played six Bach pieces for about 45 minutes. During that time, since it was rush hour, it was calculated that thousand of people went through the station, most of them on their way to work. Three minutes went by and a middle aged man noticed there was musician playing. He slowed his pace and stopped for a few seconds and then hurried up to meet his schedule. A minute later, the violinist received his first dollar tip: a woman threw the money in the till and without stopping continued to walk. A few minutes later, someone leaned against the wall to listen to him, but the man looked at his watch and started to walk again. Clearly he was late for work. The one who paid the most attention was a 3 year old boy. His mother tagged him along, hurried but the kid stopped to look at the violinist. Finally the mother pushed hard and the child continued to walk tur...