Interface: Set Stay organized with collections Save and categorize content based on your preferences.
rules. Set
Set type.
A set is an unordered collection. A set cannot contain duplicate items.
There is no set literal for use in creating sets. Instead, create sets from lists usingList.toSet(). Seerules.List.
// Create a set and check its size['a','b'].toSet().size()==2
In addition to the methods listed below, sets have the following operators:
| Operator | Usage |
|---|---|
x == y | Compare sets x and y |
v in x | Check if value v exists in set x. For example: 'a' in ['a','b'].toSet() == true |
Methods
difference
difference() returns rules.Set
Returns a set that is the difference between the set callingdifference() and the set passed todifference(). That is, returns a set containing the elements in the comparison set that are not in the specified set.
If the sets are identical, returns an empty set (size() == 0).
- Returns
non-nullrules.Setdifference set containing the elements found in the comparison set that are not gound in the calling set.
Example
['a','b'].toSet().difference(['a','c'].toSet()) == ['b'].toSet()hasAll
hasAll() returns rules.Boolean
Test whether the set callinghasAll() contains all of the items in the comparison set passed tohasAll().
- Returns
non-nullrules.Booleanwhether the calling set contains all the items of the comparison set or list.
Example
['a','b'].toSet().hasAll(['a','c']) == false['d','e','f'].toSet().hasAll(['d','e']) == truehasAny
hasAny() returns rules.Boolean
Test whether the set callinghasAny() contains any of the items in the set or list passed tohasAny().
- Returns
non-nullrules.Booleanwhether the calling set contains any of the items of the comparison set or list.
Example
['a','b'].toSet().hasAny(['c','d'].toSet()) == false['a','b'].toSet().hasAny(['a','c'].toSet()) == truehasOnly
hasOnly() returns rules.Boolean
Test whether the set callinghasOnly() contains only the items in the comparison set or list passed tohasOnly().
- Returns
non-nullrules.Booleanwhether the calling set contains only the items of the comparison set or list.
Example
['a','b'].toSet().hasOnly(['a','c']) == false['a','b'].toSet().hasOnly(['a','b']) == trueintersection
intersection() returns rules.Set
Returns a set that is the intersection between the set callingintersection() and the set passed tointersection(). That is, returns a set containing the elements the sets have in common.
If the sets have no elements in common, returns an empty set (size() == 0).
- Returns
non-nullrules.Setintersection set containing the elements found in both the calling set and the comparison set.
Example
['a','b'].toSet().intersection(['a','c'].toSet()) == ['a'].toSet()size
size() returns rules.Integer
Returns the size of the set.
- Returns
non-nullrules.Integerthe number of values in the specified set.
union
union() returns rules.Set
Returns a set that is the union of the set callingunion() and the set passed tounion(). That is, returns a set that contains all elements from both sets.
- Returns
non-nullrules.Setunion set containing all of the elements in both the calling set and comparison set.
Example
['a','b'].toSet().union(['a','c'].toSet()) == ['a', 'b', 'c'].toSet()Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-11-14 UTC.