Solved

using maps

Posted on 2012-03-14
4
152 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

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

Suggested Solutions

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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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