?
Solved

Static final HashMap.

Posted on 2006-11-20
5
Medium Priority
?
3,284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

777 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