Collections and Gosu enhancements
Enhancements on Gosu collections and related types
Gosu collection and list classes rely on collection classes from the Java language. However, Gosu collections and lists have additional, built-in enhancements compared to Java. Gosu enhancements are Gosu methods and properties added to classes or other types without the subclassing that other languages require to make use of the new methods and properties.
See also
List of enhancement methods and properties on collections
The following tables list the Gosu enhancements to collections and lists, all of which are
contained in he gw.lang.enhancements package. The letter
T refers to the type of the collection. The syntax
<T> comes from the language feature known as generics. For example,
for an argument that is listed as conditionBlock(T):Boolean, the argument
is a block. That block must take exactly one argument of the list’s type
(T) and return a Boolean.
T appears more than once in
arguments or a return result, the letter represents the same type in each usage.Most collection methods are implemented on Iterable, which is the parent interface of Collection, rather than on Collection or other types that extend Iterable.
Enhancement methods and properties on
Iterable<T>
The following table lists the additional
methods and properties for iterable objects, which are objects that implement
Iterable<T>. The Gosu enhancements are defined in
gw.lang.enhancements.CoreIterableEnhancement.
var arrList = {1, 3, 9} // ArrayList<Integer> implements Iterable|
Method |
Description |
Example |
|---|---|---|
|
|
Iterates each element of the iterable object. |
The output is:
|
|
|
Iterates each element of the iterable object with an index. |
The output is: |
|
|
Returns a new |
The output is:
|
var arrList = {1, 3, 9} // ArrayList<Integer> implements Iterable|
Method |
Description |
Example |
|---|---|---|
|
|
Removes all elements that satisfy the given criteria. This method returns no value, so it cannot be chained in series. The mutation takes place in the existing iterable object, rather than a new object being created with the specified elements removed. |
The output is:
|
|
|
Removes all elements that do not satisfy the given criteria. This method returns no value, so it cannot be chained in series. The mutation takes place in the existing iterable object, rather than a new object being created with only the specified elements. |
The output is:
|
|
|
Returns a new |
The output is:
|
|
|
Returns a new |
The output is:
|
var arrList = {1, 3, 9} // ArrayList<Integer> implements Iterable|
Method or property |
Description |
Example |
|---|---|---|
|
|
Returns |
|
|
|
Returns the number of elements in the |
|
|
|
Returns the number of elements in the |
|
|
|
Returns |
|
|
|
Returns |
|
|
|
If there is only one element in the |
|
|
|
If there is only one element in the collection that matches the given condition,
this method returns that value. Otherwise, it throws an
|
The output is:
|
var arrList = {1, 3, 9} // ArrayList<Integer> implements Iterable|
Method |
Description |
Example |
|---|---|---|
|
|
With no argument, returns the average of the numeric values in the collection. With a block argument, returns the average of the numeric values derived from the collection. |
The output is: |
|
|
Returns the first element in the |
The output is: |
|
|
Returns the first element in the collection that satisfies the condition, or
returns |
The output is: |
|
|
Returns the last element in the collection, or |
The output is: |
|
|
Returns the last element in the collection that matches the given condition, or
|
The output is: |
|
|
Returns the maximum of the selected values from the collection. This method excludes any null values. If there are no non-null values, this method throws an exception. With a block argument, returns the maximum of the values derived from the collection. |
The output is: |
|
|
Returns the maximum |
The output is: |
|
|
Returns the minimum of the selected values from the collection. This method excludes any null values. If there are no non-null values, this method throws an exception. With a block argument, returns the minimum of the values derived from the collection. |
The output is: |
|
|
Returns the minimum |
The output is: |
|
|
Returns the sum of the numeric values selected from the collection. |
The output is: |
var arrList = {1, 3, 9} // ArrayList<Integer> implements Iterable|
Method |
Description |
Example |
|---|---|---|
|
|
Maps each element of the collection to a |
Consider the following data structure:
The
|
|
|
Accumulates the values of a |
The output is: |
|
|
Joins all elements together as a string with a delimiter. |
The output is: |
|
|
Returns a |
The output is: |
|
|
Partitions this |
The output is: |
|
|
Accumulates the values of a |
The output is: |
|
|
If this iterable object is already a collection, this method returns the same
|
|
|
|
If this iterable object is already a list, this method returns the same
|
The output is: |
|
|
Converts the iterable object to a |
The output is: |
|
|
Converts the |
The output is: |
var arrList = {1, 3, 9} // ArrayList<Integer> implements Iterable
var arrList2 = {4, 9, 12, 12, 32}|
Method |
Description |
Example |
|---|---|---|
concat(collection<T>) |
Returns a new Collection<T> that is the
concatenation of this iterable object and the other collection |
The output is:
|
|
|
Returns a new |
The output is:
|
|
|
Returns a |
The output is: |
|
|
Returns a new |
The output is: |
|
|
Returns a new |
The output is: |
zip(iterable<R>) |
Returns a List<T,R> of
gw.util.Pair elements that contain corresponding values from
the two iterable objects. If one iterable object contains more elements than the
other one, any extra elements are discarded. |
The output is:
|
Enhancement methods and properties on
Collection<T>
The Gosu enhancements are defined in gw.lang.enhancements.CoreCollectionEnhancement.gsx.
Collection. The code examples in the table
are based on the following example:
var strArrList = {"wolf cub", "puppy", "tiger cub", "kitten"} // ArrayList implements Collection|
Method |
Description |
Example |
|---|---|---|
|
|
Returns a new |
The output is: |
|
|
Returns a new |
The output is: |
Enhancement methods on List<T>
List<T>.
The Gosu enhancements are defined in
gw.lang.enhancements.CoreListEnhancement.gsx,
gw.lang.enhancements.CoreListOfComparablesEnhancement.gsx and
gw.lang.enhancements.CoreIOrderedListEnhancement.gsx. The code examples
in the table are based on the following example:
var arrList = {1, 3, 9} // ArrayList implements List|
Method |
Description |
Example |
|---|---|---|
|
Converts the |
The output is: |
|
|
Creates a copy of the list as a |
The output is: |
|
|
Returns a new unmodifiable version of the list as a |
The output is: |
|
|
Partitions this |
The output is similar to the following lines, which have been truncated: |
|
|
Returns a |
The output is similar to the following: [3, 1, 9] |
|
|
Sorts the list in place, with optional support for localized sorting. By default, this method does not use a localized sort collation order. For localized sorting, pass a comparator as an argument to the method. This method returns the original list, sorted in place. Note: Ensure the
collection is cast to a sortable type.
|
The output is: strArrList: [kitten, puppy, tiger cub, wolf cub] newArrList: [kitten, puppy, tiger cub, wolf cub] true |
|
|
Sorts the list in place but instead of comparing the items directly like sort does, uses a block that returns the item to compare. By default, this method does not use a localized sort collation order. For localized sorting, pass a comparator as a second argument to the method. This method returns the original list, sorted in place. This method is different
from Note: Ensure the collection is cast to a
sortable type.
|
The output
is: strArrList: [puppy, kitten, wolf cub, tiger cub] newArrList: [puppy, kitten, wolf cub, tiger cub] true |
|
|
Same as Note: Ensure the collection is cast to a sortable type.
|
The output
is: strArrList: [tiger cub, wolf cub, kitten, puppy] newArrList: [tiger cub, wolf cub, kitten, puppy] true |
sortDescending(comparator) |
Same as Note: Ensure the collection is cast to a sortable type.
|
The output
is: strArrList: [wolf cub, tiger cub, puppy, kitten] newArrList: [wolf cub, tiger cub, puppy, kitten] true |
Enhancement methods on Set<T>
The following
table lists the available methods and properties on Set<T>. The Gosu
enhancements are defined in gw.lang.enhancements.CoreSetEnhancement.gsx.
Set<T>. The code
examples in the table are based on the following example:
var set = new HashSet() // A set of type Object
set.add("String 1")
set.add("String 2")|
Method |
Description |
Example |
|---|---|---|
|
Returns a copy of the set as a |
The output is: |
|
|
Returns a copy of the set as a |
The output is:
|
|
|
Returns a new unmodifiable copy of the set as a |
The output is:
|
partition() |
Partitions this |
The output is similar to the following lines, which have been truncated:
|
|
|
Returns the power set of the set as a |
The output is:
|
See also
List of enhancement methods and properties on maps
The following tables list the available enhancement methods and properties on
Map<K,V>. The symbol K represents the key class.
The symbol V represents the value class. The Gosu enhancements are defined
in gw.lang.enhancements.CoreMapEnhancement.gsx.
var mapper = new HashMap<String, String>()
mapper.put("aKey", "aValue")
mapper.put("bKey", "bValue")|
Method or property |
Description |
Example |
|---|---|---|
|
|
Returns a HashMap object that is a copy of this map object.
This method always returns a HashMap object, even if the
original map was another class that implements the |
The output is:
{bKey=bValue, aKey=aValue} |
|
|
The number of keys in the map. |
The output is:
2 |
|
|
A collection of all keys in the map. |
The output is:
{bKey, aKey} |
|
|
Returns an AutoMap object that wraps the current map and provides handling of default values. The method takes a default value mapping block with the same meaning as in the constructor for the AutoMap object. The block takes one argument (the key) and returns a default value that is appropriate for that key. |
The output
is:
Key new = Hello World length 0 mapped Key aKey = aValue length 6 mapped |
|
|
A collection of all values in the map. |
The output is:
{bValue, aValue} |
var mapper = new HashMap<String, String>()
mapper.put("aKey", "aValue")
mapper.put("bKey", "bValue")|
Method |
Description |
Example |
|---|---|---|
|
|
Runs a block for every key in the map. The block must take one argument (the key) and return no value. |
The output
is:
Key bKey = bValue Status: Reviewed Key aKey = aValue Status: Reviewed |
|
|
Runs a block for every key-value pair in the map. The block must take two arguments: a key and a value. The block must return no value. |
The output is:
Mapper | bKey:bValue | aKey:aValue |
|
|
Runs a block for every value in the map. The block must take one argument (the value) and return no value. |
The output is:
Values | bValue | aValue |
retain in the name are destructive. The methods with
filter in the name create a new map and do not modify the original map.
The code examples in the table are based on the following example:
var mapper = new HashMap<String, String>()
mapper.put("aKey", "aValue")
mapper.put("bKey", "bValue")|
Method |
Description |
Example |
|---|---|---|
|
|
Returns a new map that is a clone of the original map but without entries whose
keys do not satisfy the keyFilter expression. The key filter
block must take one argument (a key) and return |
The output is:
{aKey=aValue} |
|
|
Returns a new map that is a clone of the original map but without entries whose
values do not satisfy the valueFilter expression. The key
filter block must take one argument (a value) and return |
The output is:
{bKey=bValue} |
|
|
Returns a new map that contains the same keys as the original map but different values based on a block that you provide. The block must take one argument (the original value) and return a new value based on that value. The values can have an entirely different type. Gosu infers the return type of the block based on what you return and creates the
appropriate type of map. For example, the Gosu code in the Example column takes a
|
The output is:
{bKey=6, aKey=6} |
|
|
Destructively removes all entries whose keys do not satisfy the
keyFilter expression. Returns |
The output is:
{aKey=aValue} |
|
|
Destructively removes all entries whose values do not satisfy the
valueFilter expression. Return true if this map changed as a
result of the call. The value filter block must take one argument (a value) and
return |
The output is:
{bKey=bValue} |
var mapper = new HashMap<String, String>()
mapper.put("aKey", "aValue")
mapper.put("bKey", "bValue")name=Default values
maxAge=100
minAge=18|
Method |
Description |
Example |
|---|---|---|
|
|
Reads the contents of this map from a file in the Java properties format using
APIs on |
This example reads the text
The output
is:
{maxAge=100, minAge=18, name=Default values} |
|
|
Writes the contents of this map to a file in the Java properties format using
APIs on |
This example writes the text
File contents:
|
