I was trying to figure out a solution to a minor problem that we recently ran into with our project. In brief, we have a CFC that communicates, as a RemoteObject, with our Flex front-end. One of the methods in this CFC receives an array of objects and must store the object data in our database. There are a few scenarios where this array of objects could contain duplicates. After some discussion, we opted to have the back-end (CFML) check for and remove duplicate objects, should they exist, from the array.

This is a task I've often had to do with lists, which is quite simple with CFML alone. But with these array of objects, I had to verify that each property in the object matched another object. I considered a solution wherein I looped over the array of objects and performed the necessary comparisons on each property, and so on down the line.

While I knew I could make it work, everything I thought of felt like too much code and unnecessarily cumbersome/complex. I kept thinking that there had to be a way to use Java to help myself out. I am always looking for ways I can 'sneak' a little Java into my applications. However, not being an accomplished Java programmer (to say the least), it's really tough to pull off!

Anyway, I did a little research on removing duplicate objects from arrays in ColdFusion and found a really helpful post from Adam Presley (http://blog.adampresley.com/2009/removing-duplicate-array-items-in-coldfusion/) that got me started. Adam's post and code snippets worked great when I sent in an array of simple objects (strings, integers, and the like) but I wasn't able to remove duplicates when I passed in the array of objects. Even though it did not directly solve my problem, Adam's post helped me in that I was able to see a clear and related example of how I could use Java to my advantage in a CF project.

In terms of using Java in ColdFusion, I (too) often forget that you can just call native Java methods on ColdFusion objects. However, I am not well-versed in what those methods might be for a specific ColdFusion object. I did a little Googling and found a handy post from Christian Cantrell on how you can tell what data types a ColdFusion object inherits from Java. As noted in his post, a ColdFusion array inherits from the java.util.Vector. A 'quick' perusal of the Java API docs and I learned of two important, relevant methods available to Vectors: add and contains. 

Contains and add do exactly what you might expect. Contains checks the array to see if it already contains the passed in object and returns a boolean. Add will add the object to an array. These methods are incredibly helpful and here's how it solved my problem. 

function removeDuplicatesFromArrayOfObjects(aryOfObjects){   
var deduped_array = [];
for(i = 1; i < arrayLen(arguments.aryOfObjects); i++){
if( !deduped_array.contains(arguments.aryOfObjects[i]) ){
deduped_array.add(arguments.aryOfObjects[i]); // it's so cool how you just call .add on a CF array!
}
}
return deduped_array;
}

One thing I noticed is that the contains method evaluated objects as equal regardless of the casing of a string. For example, I had two objects where one string property was "A" and another was "a". The contains method saw them as the same. There may (probably?) be a way to prevent this behavior and treat strings with differing cases as unique objects but I honestly don't know enough ... yet ;).

I haven't quite mastered Posterous's syntax highlighting and don't think it supports ColdFusion, so I converted my CFML tag-based function to a script-based one (and in the process may have messed something up ... apologies if that's the case but it seems ok).

I know this isn't an earth-shattering revelation or the most kick-ass use of ColdFusion's Java prowess but it saved me a good bit of time and for those, like me, who are often unsure of how you can leverage Java in CFML, I wanted to post my experience. Plus, it's only six freaking lines of code to find duplicate objects in an array of objects!!

Posted via web from Craig Kaminsky's posterous

Comments