Merging arrays in ColdFusion


Merging arrays in ColdFusion

Posted by Luis Majano
Nov 24, 2009 00:00:00 UTC
I need to merge several arrays today in CF but I wanted to treat the resulting merged array as a set, where it only had items that are not duplicated. Java to the rescue, our good old Set interface and using a Tree set class.

All we need to do is use its awesome methods:

a1 = [1,2,3,4,5,6];

a2 = [2,6,8,4,99,0];

set = createObject("java","java.util.TreeSet").init(a1);

set.addAll(a2);

That's it. This merges the array collections and creates a sorted set without any duplicates. Quick and easy!

 


James White

Very nice. It's good to see Java have some use in ColdFusion. I like Java but feel like I would be force fitting Java into something CF can do, for most of the stuff I have considered Java for.

marc esher

Great tip, Luis. Thanks!

Sami Hoda

You should add a UDF to CFLIB called ArrayMergeUnique or something like that. Definitely useful.

John Allen

sharp!

Site Updates

Entry Comments

Archives

Entries Search