It is fairly straightforward to create Java objects in Coldfusion:
variable = CreateObject("JAVA", "java.object").init(JavaCast("primitiveType", cfVar));
However, it isn’t as straightforward to work with Java return types if, say, a Java method returns a list of Java objects:
newVariable = variable.returnJavaObjectCollection();
Is there a best practice for working with Java objects that are returned in an array or an ArrayCollection?
If your return a
Vector<Object>it will work with ColdFusion’s array utilities, and aHashMap<String,Object>will work with ColdFusion’s Struct utilities. A few notes though:null objects are not defined in ColdFusion, so if an element in the array is null, or a value in the map is null, or you just return null, their respective variables will be undefined.
Keep in mind that you can still call java methods on complex java objects in ColdFusion–including methods on sophisticated collections besides Vectors and Maps. For example: