Solved

Static final HashMap.

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

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