Solved

using maps

Posted on 2012-03-14
4
150 Views
Last Modified: 2012-03-14
If I need to match up responses to requests upon receipt, is the best way to use a map?
Is there a specific kind of map that would be best suited for this?
Both the REQUEST and RESPONSES are XML getting passed to/from a JMS service.
I want to use the jms message id to match the pairs.
Can I see an example of how to do this?
0
Comment
Question by:dpalyca755
  • 4
4 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 37721493
read this and you'll now about maps:
http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

HashMap is a very convenient object when you need to maintain correspondednce betyween the key and the value

Is it good to use HashMap for your particular purposes?
This depends on tne orgniazation of your appluication
You usually want something simple - like ID in the orm of the string or integer to be used as a key
and you may use any object, including your XML text or anny othert opbject  as a value
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 37721502
If you send some string idenstifying your request and make sure that the same identifier is returned to you after request is processesd in your respinse - then you can easily use it to match up your resquest and responses using HashMaps
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 37721515
In any case get yourself familiar  with HahsMap class - along with ArrayList this is the secend most used  collection in Java and it can without any doubts be very useful in many cases which come up in java programming. You should have it in your arsenal without question and don't think about it as something special
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 37721640
I think you'll in most cases have natural key which will match your request and response
When you post your request - it is most probably request of information about some object, so you send the id of the object, and request some information about i=it (say you send the social security number of a person, and you get back the name and date of birth and any other information) - in your response you'll again usually  usually receive your id as the header and the result in the form of a string or say it may be parsed into several strings.
So on the side when you receive the result - you populate a HashMap which has id as a key and the String with all the data or the custom object of the class where each piece of information has its own field as a vlaue. So this HashMap will store the contents of all your responses. Then afterwards you can go through the whole list of your ID's and retrieve from HashMap the information correspodning to that id.
So  once you have a list of all ID's, and a HashMap of all responses, you can easily retireive information for each valoe of ID as you go through the list of your objects.

Keep in mind that your ID shoul be unique as HashMap cannot have two Key-Value pairs with the same Key. As soon as you use put() method with the key which already exists in this HashMap the old value corresponding to this key will be overwritten with the new value
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 33
Basic Java Case or If-Else statement... 3 43
split string containing \r\n in Java 46 38
xampp tool 12 30
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

861 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now