Solved

using maps

Posted on 2012-03-14
4
153 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
[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
  • 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
mysql jsp example issue 32 93
JAVA API design with micro service cloud in mind 1 106
DTD and JAVA versions 1 53
Java array 10 85
In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …
Suggested Courses

738 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