Solved

Static final HashMap.

Posted on 2006-11-20
5
3,206 Views
Last Modified: 2008-11-26
Hi all,

I want to create a hashmap to use as a lookup table. I want it defined close to the code that uses it. Ideally, I'd like it to look something like:

<what goes here?> lookup = {
 {1, "Message 1"},
 {2, "Message 2"},
 {12, "Message 12"},
};
String message = lookup.get(messageNumber);
if ( message != null ) ...

Can you fill in the <what goes here?> bit?

It will be a simple table, no more than about 30 entries. It wont be used often so efficiency is not an issue.

Thanks all.

Paul

0
Comment
Question by:PaulCaswell
  • 3
  • 2
5 Comments
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17978042
It doesnt actually have to be a HashMap. All I really want is to lookup a number in a sparse table and get a string, or null.

Paul
0
 
LVL 92

Expert Comment

by:objects
ID: 17978077
Map<Integer, String> lu = new HashMap<Integer, String>();
lu.put(1, "Message 1");
lu.put(2, "Message 2");
lu.put(12, "Message 12");
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17978334
Thanks objects,

This is easy but the problem is that this requires:

1. A class instance hashmap, to ensure that it is only initialised once.
2. A method to initialise it.
3. An invocation.

And they are all in different places in the code. I want them all together in one place so that when someone comes along wanting to change the code, or repeat the technique they can just copy the code and fill in the new table.

Something like this is what you are suggesting I believe:

class MyClass ...
 ...
 HashMap lookup; // The class instance.

 ...
 void aMethod () {
   ...
   String message = getMessage(id); // The invocation.
   ...
 }

 String getMessage ( int id ) { // The lookup method.
  String message = "Not Found";
  if ( lookup == null ) {
    lookup = new HashMap ();
    lookup.put(1, "Message 1");
    ...
  }
  String found = (String)lookup.get(id);
  if ( found != null ) message = found;
  return message;
 }

As you can see, this breaks the lookup functionality into three separate places. I would like it all in just one place.

If I was doing this in C++ I would do something like:

struct { int id; char * message } [] table = {
  { 1, "Message 1"},
  ...
};
char * message = "Not Found";
for ( int i = 0; i < lengthof(table); i++ ) if ( table[i].id == id ) message = table[i].message;

Do you see what I mean by keeping all the code together?

Paul
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 17978379
u mean more like:

static final Map<Integer, String> lu = new HashMap<Integer, String>();
static
{
   lu.put(1, "Message 1");
   lu.put(2, "Message 2");
   lu.put(12, "Message 12");
}
0
 
LVL 16

Author Comment

by:PaulCaswell
ID: 17978459
A static initialiser!!! Of course! <Sound of slapped forehead>

Thank you! :-)

Paul
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now