11 Replies - 2489 Views - Last Post: 12 May 2011 - 08:50 AM Rate Topic: -----

#1 Vishu26   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 11

retrieve values of hashmap in java

Posted 09 May 2011 - 08:47 AM

I have a XML file with many copies of table node structure as below:
<databasetable TblID=”123” TblName=”Department1_mailbox”>
<SelectColumns>
	<Slno>dept1_slno</Slno>
	<To>dept1_to</To>
	<From>dept1_from</From>
	<Subject>dept1_sub</Subject>
	<Body>dept1_body</Body>
	<BCC>dept1_BCC</BCC>
	<CC>dept1_CC</CC>
</SelectColumns>
<WhereCondition>MailSentStatus=’New’</WhereCondition>
<UpdateSuccess>
	<MailSentStatus>’Yes’</MailSentStatus>
	<MailSentFailedReason>’Mail Sent Successfully’</MailSentFailedReason>
</UpdateSuccess>
<UpdateFailure>
	<MailSentStatus>’No’</MailSentStatus>
	<MailSentFailedReason>’Mail Sending Failed ’</MailSentFailedReason>
</ UpdateFailure>
</databasetable>

As it is not an efficient manner to traverse the file for each time to fetch the details of each node for the queries in the program, I used the nested hashmap concept to store the details while traversing the XML file for the first time. The structure I used is as below:
MapMaster
Key			Value
123			MapDetails
			Key				Value
			TblName			Department1_mailbox
			SelectColumns			mapSelect
							Key			Value
							Slno			dept1_slno
							To			dept1_to
							From			dept1_from
							Subject			dept1_sub
							Body			dept1_body
							BCC			dept1_BCC
							CC			dept1_CC
			WhereCondition		MailSentStatus=’New’
			UpdateSuccess			mapUS
MailSentStatus		’Yes’
MailSentFailedReason	’Mail Sent Successfully’
			UpdateFailure			mapUF
MailSentStatus		’No’
MailSentFailedReason	’Mail Sending Failed’

But the problem I’m facing now is regarding retrieving the Value part using the nested Keys. For example,
If I need the value of Slno Key, I have to specify TblID, SelectColumns, Slno in nested form like:

Stirng Slno = ((HashMap)((HashMap)mapMaster.get(“123”))mapDetails.get(“SelectColumns”))mapSelect.get(“Slno”);


This is unconvinent to use in a program. Please suggest a solution but don’t tell that iterators are available. As I’ve to fetch the individual value from the map according to the need of my program.
Thanks in advance,

Is This A Good Question/Topic? 0
  • +

Replies To: retrieve values of hashmap in java

#2 n8schatten   User is offline

  • D.I.C Regular
  • member icon

Reputation: 146
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: retrieve values of hashmap in java

Posted 09 May 2011 - 08:57 AM

Wrap it into a method!
First idea (Semi-Java-Pseudo-Code):
public String getValueFor(String... keys) {
  Map<String, ?> map = null;
  int keysAmount = keys.length;
  for (int i = 0; i < (keysAmount -1); i++) {
    map = map.get(keys[i]);
  }
  return map.get(/*last element of keys*/);
}


Just a sketch. Currently no possibility to try it in action. Sorry for that! Most probably you have to do some casting. But at least it's located in one single method and not at each place you need to get some value.

This post has been edited by n8schatten: 09 May 2011 - 09:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 darek9576   User is offline

  • D.I.C Lover

Reputation: 204
  • View blog
  • Posts: 1,740
  • Joined: 13-March 10

Re: retrieve values of hashmap in java

Posted 09 May 2011 - 09:08 AM

You can use an Iterator like the following:

private Map<String,Something> map = new HashMap<String,Something>();

Iterator<String> it = map.keySet().iterator();

while(it.hasNext())
{
   //do something

}




However, this iterator will only output the keys.
Good luck.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11984
  • View blog
  • Posts: 20,306
  • Joined: 19-March 11

Re: retrieve values of hashmap in java

Posted 09 May 2011 - 09:16 AM

It might be easier to define some classes than to use nested HashMaps.

But if you're going to use HashMap, don't do this:

HashMap innerMap = new HashMap();
HashMap middleMap = new HashMap();
HashMap outerMap = new HashMap();


Which is what I suspect you're doing, judging by the casts I see. Instead, you want something like

HashMap<String, String> innerMap = new HashMap()<String, String>;
HashMap <String, Hashmap <String, String>> middleMap =    
   new HashMap<String, Hashmap <String, String>>();
HashMap<String, HashMap<String, Hashmap <String, String>>>  outerMap = 
   new HashMap<String, HashMap<String, Hashmap <String, String>>>();


Which is more ugly in the declaration, but gives you safer code and avoids those casts.

But I do think you want objects, not maps.
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3703
  • View blog
  • Posts: 16,975
  • Joined: 20-September 08

Re: retrieve values of hashmap in java

Posted 09 May 2011 - 11:32 AM

Quote

As it is not an efficient manner to traverse the file for each time to fetch the details of each node for the queries in the program


Why would you need to traverse it? All you need to do is hold the DOM in memory

Quote

I used the nested hashmap concept to store the details while traversing the XML file for the first time


In doing so, you're really only reinventing the wheel that is that DOM (badly)

What's more, once you have the DOM, getting the required nodes is trivial: use XPath

This post has been edited by g00se: 09 May 2011 - 11:33 AM
Reason for edit:: xpath

Was This Post Helpful? 2
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: retrieve values of hashmap in java

Posted 09 May 2011 - 03:22 PM

I agree with g00se. DOM sounds like a good solution to me. In fact, KYA wrote a blog entry on it.

If you want to parse all the data, at least organize it more effectively. A Tree is more appropriate than nested Maps (at which point though, might as well just stick with DOM). Or Objects modeling the data represented in your XML file might be more appropriate as well. If you are going to be using this data throughout your program, reading them into Objects of classes you designed is the better option.
Was This Post Helpful? 1
  • +
  • -

#7 Vishu26   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 11

Re: retrieve values of hashmap in java

Posted 10 May 2011 - 08:54 AM

My program is all about:
1. Fetch the IDs of departments which have mail sending privileges.
2. Compare the IDs with the IDs in XML file.
3. Fetch the details of only those node which have satisfied the comparison condition.

To define a class for the XML structure the data members must be constant in numbers( I think so ). But my XML file may vary in its number w.r.t its text nodes. To illustrate, Later sometime I may add the below structure:
<UpdateSuccess>
<MailSentStatus>'Y'</MailSentStatus>
<MailSentFailure>'Mail Sent Successfully'</MailSentFailure>
<LastUpdateOn>'25/05/11'</LastUpdateOn>
</UpdateSuccess>

This means I can add or remove the update statements as required by the situation. I'm not clear about the use of class implementation for the XML. Please will you suggest how can I've the class definition for the XML structure.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: retrieve values of hashmap in java

Posted 10 May 2011 - 09:03 AM

What are you modeling with your XML? An email message? If so, define an EmailMessage class to model that data, and read the XML file contents into EmailMessage objects, and store a List<EmailMessage>. If the EmailMessage object you create satisfies the conditions, add it to the List<EmailMessage>. Otherwise, proceed to the next element.
Was This Post Helpful? 1
  • +
  • -

#9 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3703
  • View blog
  • Posts: 16,975
  • Joined: 20-September 08

Re: retrieve values of hashmap in java

Posted 10 May 2011 - 09:10 AM

Quote

To define a class for the XML structure the data members must be constant in numbers


No, that's not the case - they would simply be modelled as a collection.

Have a look at JAXB or for a 'lighter' approach, you could look at Commons Digester
Was This Post Helpful? 1
  • +
  • -

#10 Vishu26   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 11

Re: retrieve values of hashmap in java

Posted 10 May 2011 - 09:21 AM

View Postmacosxnerd101, on 10 May 2011 - 09:03 AM, said:

What are you modeling with your XML? An email message? If so, define an EmailMessage class to model that data, and read the XML file contents into EmailMessage objects, and store a List<EmailMessage>. If the EmailMessage object you create satisfies the conditions, add it to the List<EmailMessage>. Otherwise, proceed to the next element.


Thank you... But I'm not understanding your suggestion clearly as I'm new to Java and the class implementation with List structure. I've dealt with basic datatypes and now has understood hashmap with difficulty. If there would be any illustration available then it would be more helpful. Don't mistake me, I'm not asking the code for the class definition w.r.t my problem. If there is any implementation available, for the problem similar to mine, in any site that you might have come accross, then it would be helpful.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12769
  • View blog
  • Posts: 45,954
  • Joined: 27-December 08

Re: retrieve values of hashmap in java

Posted 10 May 2011 - 09:22 AM

I have a tutorial on class design that you may find helpful. :)
Was This Post Helpful? 1
  • +
  • -

#12 Vishu26   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-May 11

Re: retrieve values of hashmap in java

Posted 12 May 2011 - 08:50 AM

View Postmacosxnerd101, on 10 May 2011 - 09:22 AM, said:

I have a tutorial on class design that you may find helpful. :)


Thank you very much for your kind suggestion and help. I am able to design a class with hashmap as datamember and my own get methods to fetch the data stored in it. Hope I will get the same support and suggestions to any of my doubts in future. Thanks a lot. :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1