The nlib_List class provides a fluent, convenient wrapper for working with arrays of data.
It allows you to reduce the boilerplate code required for many of the typical patterns required when
working with native APEX lists.
For example we can easily create a new collection of Contact records and determine the most recent created date.
Date latestDate = nlib_List.collect([SELECT Id, CreatedDate FROM Contact]).pluck('CreatedDate').max();
No loops required!
As mentioned above, the collect
method returns a new collection instance. Creating a new collection is as simple as:
nlib_List collection1 = nlib_List.collect(); // Empty collection
nlib_List collection2 = nlib_List.collect(new Contact(LastName = 'Jones')); // Collection of SObjects
nlib_List collection3 = nlib_List.collect(new Contact[]{}); // Empty collection of SObjects
nlib_List collection4 = nlib_List.collect( 'A' ); // Collection of strings
nlib_List collection5 = nlib_List.collect(new String[]{ 'A', 'B', 'C' }); // Collection of strings
nlib_List collection6 = nlib_List.collect( 1 ); // Collection of numbers
nlib_List collection7 = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }); // Collection of numbers
nlib_List collection8 = nlib_List.collect(Date.today()); // Collection of dates
The combine
method can also be used to merge two existing arrays or collections into a new list.
Integer lst1 = new Integer[]{ 1, 2, 3, 4, 5 };
Integer lst2 = new Integer[]{ 6, 7, 8, 9 };
nlib_List lst3 = nlib_List.combine( lst1, lst2 ); // outputs new collection [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
The add
method appends a value, list of values, or another collection to the end of the collection.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3 });
collection.add( 4 ); // [ 1, 2, 3, 4 ]
collection.add( new Integer[]{ 5, 6, 7 }); // [ 1, 2, 3, 4, 5, 6, 7 ]
collection.add(nlib_List.collect( 8 )); // [ 1, 2, 3, 4, 5, 6, 7, 8 ]
The addUnless
method appends a value to the end of the collection if the boolean expression evaluates to false
.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3 });
collection.add( 4, true ); // value not appended [ 1, 2, 3 ]
collection.add( 4, false ); // value appended [ 1, 2, 3, 4 ]
The addWhen
method is the opposite of addUnless
.
It appends a value to the end of the collection if the boolean expression evaluates to true
.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3 });
collection.add( 4, true ); // value not appended [ 1, 2, 3 ]
collection.add( 4, false ); // value appended [ 1, 2, 3, 4 ]
Returns the underlying array represented by the collection.
The array is returned as a list of generic objects so it needs to be cast to the intended type.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3 });
(Integer[]) collection.all(); // returns [ 1, 2, 3 ]
Returns the average value of an array of numbers.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3 });
collection.avg(); // returns 2.0
The chunk
method breaks the collection into multiple, smaller lists of a given size.
Returns type is List<nlib_List>
.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9 });
collection.chunk( 5 ); // returns [ 1, 2, 3, 4, 5 ] [ 6, 7, 8, 9 ]
Returns a reference copy of the collection.
nlib_List collection1 = nlib_List.collect(new Integer[]{ 1, 2, 3 });
nlib_List collection2 = collection1.copy();
Returns a new cloned instance of the collection.
nlib_List collection1 = nlib_List.collect(new Integer[]{ 1, 2, 3 });
nlib_List collection2 = collection1.deepCopy();
Returns the difference between two collections.
Compares the original collection against a given collection. Returns a new list containing the values in the original collection not found in the given collection.
nlib_List collection1 = nlib_List.collect(new Integer[]{1,2,3,4,5,6,7,8,9,0});
nlib_List collection2 = nlib_List.collect( new Integer[]{ 4, 5, 6, 7 } );
collection1.diff( collection2 ); // returns [ 1, 2, 3, 8, 9, 0 ]
Logs the underlying array of values to the debug log. Useful to log the current state of the collection while inside a fluent chain.
nlib_List collection1 = nlib_List.collect(new Integer[]{ 1, 2, 3 })
.add( 4 )
.dump() // Logs [ 1, 2, 3, 4 ]
.add( 5 )
.dump() // Logs [ 1, 2, 3, 4, 5 ]
.add( 6 )
.all(); // outputs [ 1, 2, 3, 4, 5 ];
Compares this collection to the given array. Returns true
if they are the same.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3 });
collection.equals(new Integer[]{ 1, 2, 3 }); // true
Compares this collection to the given array. Returns true
if they are different.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3 });
collection.notEquals(new Integer[]{ 2, 3, 4 }); // true
Returns a new collection without the specified elements.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
nlib_List newCollection = collection.except(new Integer[]{ 2, 3 }); // [ 1, 4, 5 ]
Returns the index of the given element in the collection.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.find( 4 ); // 3
Returns the first element in the collection.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.first(); // 1
Returns the first element in the collection or the given value if the list is empty.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.firstOr( 6 ); // 1
nlib_List emptyCollection = nlib_List.collect();
emptyCollection.firstOr( 6 ); // 6
Returns the first element in the collection or null
if the collection is empty.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.firstOrNull(); // 1
nlib_List emptyCollection = nlib_List.collect();
emptyCollection.firstOrNull(); // null
Returns the element at the given index.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.get( 2 ); // 3
Returns the element at the given index or the given value if the index is out of bounds.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.getOr( 2, 6 ); // 3
collection.getOr( 10, 6 ); // 6
Returns the element at the given index or null
if the index is out of bounds.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.getOrNull( 2 ); // 3
collection.getOrNull( 10 ); // null
Returns the system type of the elements in the collection.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.getType(); // Integer.class
Returns the system type name of the elements in the collection.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 });
collection.getType(); // 'Integer'
Throws the given exception if the list is empty, otherwise it returns itself.
This allows it to be used in a fluent chain of methods.
nlib_List collection = nlib_List.collect();
collection.ifEmptyThrow(new MyException('Error message')); // FATAL_ERROR|MyException: Error message
Returns true
if the collection is empty.
nlib_List.collect().isEmpty(); // true
Returns true
if the collection is not empty.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).isNotEmpty(); // true
Returns the last element in the collection.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).last(); // 5
Returns the last element in the collection or the given value if the list is empty.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).lastOr( 6 ); // 5
nlib_List.collect().lastOr( 6 ); // 6
Returns the last element in the collection or null
if the list is empty.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).lastOrNull(); // 5
nlib_List.collect().lastOrNull(); // null
Returns the index of the last element in the collection.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).lastIndex(); // 4
Returns the maximum value of the collection.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).max(); // 5
Returns the minimum value of the collection.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).min(); // 1
For use with SObject collections.
Returns a new collection containing just the values in the given SObject field name.
nlib_List collection = nlib_List.collect();
collection.add(new Contact(LastName = 'Smith', MailingState = 'WA'));
collection.add(new Contact(LastName = 'Jones', MailingState = 'NY'));
nlib_List states = collection.pluck('MailingState'); // [ 'WA', 'NY' ]
For use with SObject collections.
Returns a new collection containing the Ids from the records in the collection.
nlib_List collection = nlib_List.collect([SELECT Id, LastName FROM Contact]);
nlib_List ids = collection.pluckIds(); // [ 003R000001KHEJXIA5, 003R000001KHEJYIA5 ]
Adds the given element to the beginning of the collection.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).prepend( 6 ); // [ 6, 1, 2, 3, 4, 5 ]
Removes and returns the element at the given index in the collection.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).pull( 2 ); // returns 3 and the remaining collection is [ 1, 2, 4, 5 ]
Removes the element at the given index in the collection. Returns the remaining collection.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).remove( 2 ); // [ 1, 2, 4, 5 ]
Returns a new collection with the elements in reverse order.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).reverse(); // [ 5, 4, 3, 2, 1 ]
Returns the number of elements contained in the collection.
nlib_List.collect(new String[]{ 'A','B','C' }).size(); // 3
Returns the elements starting from the given index to the end of the collection.
Include an optional second parameter to limit the number of elements returned.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).slice( 2 ); // [ 3, 4, 5 ]
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).slice( 2, 2 ); // [ 3, 4 ]
Splits the collection evenly into the given number of groups. Returns a list of collections.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }).split( 3 ); // [ [ 1, 2, 3, 4 ], [ 5, 6, 7 ], [ 8, 9, 10 ] ]
For use with collections of numerical values. Returns the sum of the values in the collection.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).sum(); // 15
Returns the collection serialized into the json format.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).toJson(); // [1,2,3,4,5]
Returns the collection serialized into the json format with nicer formatting.
nlib_List.collect(new Integer[]{ 1, 2, 3, 4, 5 }).toJson(); // [ 1, 2, 3, 4, 5 ]
Returns a new collection with only unique values
nlib_List.collect(new Integer[]{ 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 }).unique(); // [ 1, 2, 3, 4, 5 ]
Similar to the all()
method. Returns all the values in the collection.
nlib_List collection = nlib_List.collect(new Integer[]{ 1, 2, 3 });
(Integer[]) collection.values(); // returns [ 1, 2, 3 ]