Solved

Static final HashMap.

Posted on 2006-11-20
5
3,244 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

809 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