Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Static final HashMap.

Posted on 2006-11-20
5
Medium Priority
?
3,295 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

609 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