Solved

using maps

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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 …

705 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