Merging arrays in ColdFusion
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!