Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing a hashtable

 
Christopher Frankland
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am executing a bit of code that returns a bunch of values as a hashtable:



Output:

{departmentScore={Floor=8.1, Pharmacy=7.6, Cashier=7.1}, monthlyScore={August=8.7, July=8.3, November=8.4, June=8.1, October=7.5, April=9.5, May=9.1, March=7.8, January=7.3, February=6.6, September=6.4}, groupScore={Store=7.9, District=7.8, Company=7.9}, attributeScore={Grooming=8.0, Rx=9.8, Line=8.2, Take=6.7, Convenience=9.6, Thank=8.0, Anticipate=7.7, Rx Line=8.8, Greet=7.8}}

I need to somehow divide the above into separate values so I can create a String containing XML elements. i.e:

<set name="Floor" value="8.1">
<set name="Pharmacy" value="7.6">
etc...

The piece I am having difficulty with is how to break down the hashtable into individual pieces so I am put them into my string.

Any help much appreciated!

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

"Parsing" implies converting something from text into structured data, but it looks to me as if you've already got the data you need, and there's no text involved. It looks like you have a Hashtable whose keys are "departmentScore", "monthlyScore", "groupScore", etc, and the corresponding values are other Hashtables nested inside the big one; the "departmentScore" Hashtable contains keys like "Floor", "Pharmacy", and "Cashier", with values 8.1, 7.6, 7.1. So you can just do something like

StringBuffer buffer = new StringBuffer();
for (Iterator entries = result.getEntries(); entries.hasNext() {
Map.Entry entry = (Map.Entry) entries.next();
String key = (String) entry.getKey();
Hashtable value = (Hashtable) entry.getValue();
for (Iterator entries2 = value.getEntries(); entries2.hasNext() {
Map.Entry entry2 = (Map.Entry) entries2.next();
Object key2= entry2.getKey();
Object value2 = entry2.getValue();
buffer.append("<set name=\"");
buffer.append(key);
buffer.append("\" value=\"");
buffer.append(value);
buffer.append("\"/>\n");
}
}
 
Christopher Frankland
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I will give that a go.

Although, I notice there isn't a getEntries() method on Hashtable?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called entrySet(), and it returns a Set of Map.Entry objects. You'll need to call iterator() on that set as well.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rob. Why doesn't Firefox have built-in Java API completion?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic