Java Hashtable - An Object-Based-Lookup "Struct" (Thanks Nathan Mische)

Posted September 24, 2008 at 9:42 AM by Ben Nadel

Tags: ColdFusion

A little while ago Nathan Mische solved a problem that some people were getting with my POI Utility project in which cell styles were "bleeding" into other cells. The problem, as he discovered, had to do with my reliance on the undocumented .hashCode() method of ColdFusion structs, which apparently was not the most reliable undocumented method on the block. To solve this problem, he used the Java Hashtable object to cache CellStyle instances. This was quite clever. I will definitely be implementing this, but before I do, I figured I better play around with the Java Hashtable a bit to see how it works.

The Java Hashtable acts much like a ColdFusion struct in that it stores and returns values based on keys. The important difference is that ColdFusion structs use string-based keys and Java Hashtables use "object" keys. This means we can use any value (that supports the .hashCode() and .equals() methods) as our value key.

Ok, so let's start out by just creating some structs to use as the key for some arrays:

  • <!--- Set up a key. --->
  • <cfset objKeyA = {
  • FirstName = "Ben",
  • LastName = "Nadel"
  • } />
  •  
  • <!--- Set up a value object. --->
  • <cfset objValueA = [
  • "Ben",
  • "Nadel"
  • ] />
  •  
  •  
  • <!--- Set up a key. --->
  • <cfset objKeyB = {
  • FirstName = "Sarah",
  • LastName = "Vivenzio"
  • } />
  •  
  • <!--- Set up a value object. --->
  • <cfset objValueB = [
  • "Sarah",
  • "Vivenzio"
  • ] />
  •  
  •  
  • <!--- Create a Java hash table with default load settings. --->
  • <cfset objHashTable = CreateObject(
  • "java",
  • "java.util.Hashtable"
  • ).Init()
  • />
  •  
  • <!---
  • Put both values in the hash table based on their
  • key objects.
  • --->
  • <cfset objHashTable.Put( objKeyA, objValueA ) />
  • <cfset objHashTable.Put( objKeyB, objValueB ) />
  •  
  •  
  • <!--- Dump out the first value. --->
  • <cfdump
  • var="#objHashTable.Get( objKeyA )#"
  • label="ValueA Using Original Key"
  • />
  •  
  •  
  • <!--- Dump out the second value. --->
  • <cfdump
  • var="#objHashTable.Get( objKeyB )#"
  • label="ValueB Using Original Key"
  • />

As you can see, we are caching the two arrays based on their relative structs. Then, we simply CFDump them out again, using the original structs as the look-up keys. Running the above code, we get the following output:

 
 
 
 
 
 
Java Hashtable Demo Using ColdFusion. 
 
 
 

This gives us back the same values we put in.

But, what if we create a totally new "key" struct? Is it the actual instance of the struct that is important when it is used as a look-up key, or is the contents of the struct that are more important? To test this, we are going to create a totally new struct using a different creation methodology and give it the same values our one of our original structs. Then, we're going to see what that gives us:

  • <!---
  • Now, let's create a new key that has the same values
  • as the second key, but is a new object (we are going
  • to use a different creation method to see if that makes
  • any difference).
  • --->
  • <cfset objKeyBAlt = StructNew() />
  • <cfset objKeyBAlt.FirstName = "Sarah" />
  • <cfset objKeyBAlt.LastName = "Vivenzio" />
  •  
  • <!--- Dump out the second value using new key. --->
  • <cfdump
  • var="#objHashTable.Get( objKeyBAlt )#"
  • label="ValueB Using Alternate Key"
  • />

Using the alternate key, we get the following CFDump output:

 
 
 
 
 
 
Java Hashtable Demo Using ColdFusion. 
 
 
 

As you can see, even when we create a totally new struct, because it has the same values, it acts as the same key. It is this feature that makes this the viable POI solution that Nathan Mische came up with.

But what about case sensitivity? ColdFusion, as we all know, is not case sensitive; Ben is the same as BEN is the same as BeN. But, does this case insensitivity bubble up to the underlying Java methods for hashCode() and Equals()? To test this, we will again create a completely new struct, but rather than using the exact same keys, we will use all uppercase keys:

  • <!---
  • Now, let's create an alternate key that is the same values
  • but with different case. ColdFusion is not case sensitive,
  • but let's see if that bubbles up to its hashCode() and
  • equals() core methods.
  • --->
  • <cfset objKeyBAlt2 = {
  • FirstName = "SARAH",
  • LastName = "VIVENZIO"
  • } />
  •  
  • <!--- Dump out the second value using new key. --->
  • <cfdump
  • var="#objHashTable.Get( objKeyBAlt2 )#"
  • label="ValueB Using Alternate Key (2)"
  • />

Running this code, we get the following output:

 
 
 
 
 
 
Java Hashtable Demo Using ColdFusion. 
 
 
 

Looks like the case insensitivity of ColdFusion does successfully bubble up to its Java interface, at least in this case.

This Java Hashtable is pretty cool, and will definitely serve as a performant solution for a caching issue that I was having in my POI ColdFusion custom tags. If you try to look up a key that doesn't exist, the Java Hashtable returns a NULL. I'm not going to bother demonstrating this as Java NULL values have been demonstrated on this blog a thousand times, but, if you try to "get" a value from the Hashtable that it doesn't have indexed, it returns a NULL which destroys the ColdFusion value into which it is stored.

Anyway, thanks Nathan Mische for bringing this cool object to my attention.


You Might Also Be Interested In:



Reader Comments

There are no comments posted for this web log entry.

Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools