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

The Kite Runner

Usually, I buy and read book suggested by my friends but no one suggested me to read the book "The Kite Runner by Khaled Hosseini". I neither heard about it before I accidentally bought it. I was looking for the books I planned to buy in the Chennai Bookfair 2011 and I found this book . There was something attractive, though it was not as beautiful as of many of the books cover design, it should be the cover of it, because all I could see was only the book cover. I took it in my hand and asked myself, should I buy this?, yes was the answer and I bought it. I completed reading this book last week, after keeping it the shelf for almost 2 years. I am glad I got this book. It is one of best books I read in the recent years. The phrase " For you, a thousand times over " lingering in the mind is the proof that I really enjoyed reading this book and moved so much ;) " I became what I am today at the age of twelve, on a frigid overcast day in the winter of 1975 &

சந்தோஷம் சந்தோஷம் - Vijay song

Kovil Pillai P.

New Year Resolution 2015

Another new year, another ( same! ) set of new year resolutions. I wanted to read Thirukural and set target 50 last year but couldn't achieve it. But then I used one Kural in my every blog post . New Year Resolution List: For year 2016, I should shorten it to at least 1/3 of this list. At least, make it more readable ;) Reading Books: Book reading is going to be reduced this year. I am planning to reread few of the books I read last year. Of course, will find time to read books suggested by friends. I have got Bhagavad Gita tamil version from my friend and might read this before the end of 2015. I Am Malala, The Last Temptation of Jesus Christ, Mossad, 'The Madman, The Forerunner, Spirits Rebellious', Yudhargal Varalaarum Vaazkaiyum, If You Meet The Buddha On The Road Kill Him are few of the books I enjoyed reading. By the way, I have changed the FB album title from "Books I read" to "Books I read - Not To Be Shared" ;) Accounting: How difficu