Collection Tools Reference - adampresley/cfpowertools GitHub Wiki

The collection tools in CF PowerTools provide a few methods for quickly instantiating some of the data structures available from the Java Collections Framework. These examples assume you have instantiated the CollectionTools object similar to the code below.

<cfset cfPowerToolsFactory = createObject("java", "com.adampresley.cfpowertools.Factory") />
<cfset collectionTools = cfPowerToolsFactory.getCollectionTools() />

getArrayDeque

A resizable array implementation that supports both queue or stack. For more information see http://download.oracle.com/javase/6/docs/api/java/util/ArrayDeque.html.

Parameters:

  • numElements - The number of elements to support initially (optional)

Example

<!--- As a queue --->
<cfset q = collectionTools.getArrayDeque() />
<cfset q.add("Adam") />
<cfset q.add("Sr. Software Engineer") />

<cfset item = q.poll() />
<cfloop condition="isDefined('item')">
   <cfdump var="#item#" />
   <cfset item = q.poll() />
</cfloop>

<!--- As a stack --->
<cfset s = collectionTools.getArrayDeque() />
<cfset s.push("Adam") />
<cfset s.push("Sr. Software Engineer") />

<cfset item = q.pop() />
<cfloop condition="isDefined('item')">
   <cfdump var="#item#" />
   <cfset item = q.pop() />
</cfloop>

getLinkedList

A resizable array implementation that supports both queue or stack. This is very similar to the ArrayDeque, except it supports bi-directional access. For more information see http://download.oracle.com/javase/6/docs/api/java/util/LinkedList.html.

Parameters: None

Example

<!--- Like a queue --->
<cfset q = collectionTools.getLinkedList() />
<cfset q.add("Adam") />
<cfset q.add("Sr. Software Engineer") />

<cfset item = q.poll() />
<cfloop condition="isDefined('item')">
   <cfdump var="#item#" />
   <cfset item = q.poll() />
</cfloop>

<!--- Like a stack --->
<cfset s = collectionTools.getLinkedList() />
<cfset s.push("Adam") />
<cfset s.push("Sr. Software Engineer") />

<cfset item = q.pop() />
<cfloop condition="isDefined('item')">
   <cfdump var="#item#" />
   <cfset item = q.pop() />
</cfloop>

getPriorityQueue

A queue collection of items that are ordered according to either natural order, or the order specified in comparator code. For more information on the usage of this queue see http://download.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html.

Parameters:

  • initialCapacity - The number of initial elements the queue can store (optional)
  • comparatorCode - The code that compares two object and dictates order (optional)
  • params - A set of parameters to send to the comparator code (optional)

Example

<cfset q = collectionTools.getPriorityQueue(25, "object1.priority <=> object2.priority") />
<cfset q.add({ name = "Adam", priority = 2 }) />
<cfset q.add({ name = "Jesse", priority = 1 }) />

<cfset item = q.poll() />
<cfloop condition="isDefined('item')">
   <cfdump var="#item#" />
   <cfset item = q.poll() />
</cfloop>

getTreeMap

A sorted map (or structure). This structure maintains order based on either natural order of its keys, or by custom comparator code. For more information on the usage of this TreeMap see http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html.

Parameters:

  • comparatorCode - The Groovy code that compares two object and dictates order (optional)
  • params - A set of parameters to send to the comparator code (optional)

Example

<cfset t = collectionTools.getTreeMap() />
<cfset t.put("name", "Adam") />
<cfset t.put("title", "Sr. Software Engineer") />
<cfset t.put("age", 32) />
⚠️ **GitHub.com Fallback** ⚠️