views:

224

answers:

4

I would like to have a mapping which maps two string into one string. For example: map["MainServer","Status"] return "active". What is the best way to do it in Java. Should I use HashMap which include another HashMap as its elements?

+1  A: 

It sounds like you're sending messages to a server object to get return values.

Why not create a Server class with a name and status (and all other secondary properties), set that, and map servername to server?

Then, you do something like this.

Server server = map.get(serverName);
return server.getStatus();
Stefan Kendall
I do not why but it seems simpler to me just to have an associative array which maps "server name" and "server parameter" into the value of the parameter. In your solution I have to create 2 classes. One which represents servers and another one should generate instances of the first class... May be I just did not get used to the OOP...
Roman
It's the oop solution. You never know when the server all of a sudden has more fields it needs to return. Also, the person who looks at the code later won't hate you if you use a semantic solution.
Stefan Kendall
+3  A: 

Having a map to a map means that you are doing a double lookup (semantically and in terms of cost). Is this what you actually want?

You may be better off defining a MapKeyPair class that contains X strings, and overriding equals and hashCode for them.

More generally, if the pair has an actual meaning or an abstraction, represent it via an appropriately named object.

Uri
+1, basically you want to map two strings as a key into an object, using the MapKeyPair is the best approach, though I would call it StringPair, since it's not necessarily tied to MapKeys.
Juan Mendes
+1  A: 

It seems to me, that the only important information is the value at the end.

I this case the simplest solution is to combine the strings into one single key string map["MainServerStatus"]

If you want to have all values for "MainServer" you could iterate over all elements and filter the ones, which are starting with the String "MainServer".

This is a very basic and simple solution but when you do not want to know all elements of "MainServer"so foten, you could use it. Otherwise it could slow down your application

Markus Lausberg
May be a good solution. I thought about that too. But often I will need to make a loop over all servers for a fixed parameter. Well I alway can connect "serverName" + "parameterName"...
Roman
A: 

If the total number of pairs is small, go simple: a Map from the first key to a second Map; the second Map goes from the second key to the value.

If the total number of pairs is large, performance might matter. If it does, I'd suggest the same solution as above, but choose as your first key the one with the smallest anticipated range (e.g., if the first key is one of thousands of names, and the second is one of ten predefined statuses, let the second key be the first one you look up).

If performance doesn't matter, go for design transparency: use a Pair class as the key in a single Map. (A Pair class is useful enough that you should arguably already have a well-written one by now.)

Paul Brinkley
I will have 3-5 "servers" and for every server I will have less than 20 parameters.
Roman
That's quite small, then - 100 pairs maximum. Map to Map to value, bang it out, you're all done.
Paul Brinkley