Solved

using maps

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 79
firstChar challenge 13 86
Starting to learn JAVA, 7 48
eclipse formatting 6 43
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

746 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

15 Experts available now in Live!

Get 1:1 Help Now