Preserving Order With Java's LinkedHashMap Class

The other night I ran into an issue with preserving order of keys and values being added to some ColdFusion structures. As it would be, structures in CFML don't preserve data in any consistent order. It's honestly something I've known about, but none the less, something that's never given me hassle. With that faintly in the back of my mind, I did not accept it as such initially as when I wrote my code and ran it in ColdFusion 11, everything appeared in order (probably not the best route of thinking). It wasn't until I started testing in Lucee that I realized it was all a chance of luck in how my data was being displayed to screen. In fact, further examination proved that the order was not as correct as I thought originally with ColdFusion 11 either.

So here's my scenario. . .

I've been building out various features for an archive section of my blog. As a index to that page, I wanted to capture all of my posts and display them in separated collections; ordered by [ year / month / article ]. In my case, I use ORM. So when grabbing blog posts, I'm getting an array of article objects. I loop over the array and populate a structure with the year as the key and the value being another structure. That structure is then populated with the month as it's key and an array of articles as it's value. I'll articulate this better below. I'll be using a struct of dummy articles to represent my article objects.

Consider this code. . .

Take the code above for a spin:

ColdFusion 11:

Lucee 4.5:

With ColdFusion 11, the years appear to hold a sensible order; however, the months are not so well off. Lucee on the other hand twists things all around. This was definitely not what I was trying to achieve. Luckily there is a simple, solid solution.

I had taken to Twitter the next morning where I got some great insight and suggestions from James Moberg, Adam Cameron and John Whish (thanks fellas!). John brought up a nifty feature in Lucee (and Railo) where you can preserve order in a struct by using structNew("linked"). This is cool but I've been working on a set of code that could see some cross platform usage so I needed something mutually supported. John then suggested I try Java's LinkedHashMap class and gave me reference to an article by Jeremy Gibbens.

This did the trick! Better yet, you can (as far as I can tell) treat a LinkedHashMap exactly like a CFML structure; functions and all. With that said, I didn't need to modify any of the current code really; except for where I needed to apply the LinkedHashMap instead of a normal struct.

Check out the solution. . .

Try it out.

ColdFusion 11:

Lucee 4.5:

From the output above, now I'm getting the order I was looking for. Pretty cool!

Do note that when displaying the data from a LinkedHashMap with cfdump, you might not see the desired order; but when actually printing out the data, it will retain the order in which it was first constructed. Also keep in mind that a LinkedHashMap is case sensitive!