A collection in which each distinct element occurs at most once. Two non-identical values are considered distinct only if they are unequal, according to their own definition of value equality.

A Set is a Collection of its elements. Sets may not have null elements.

A new Set may be obtained by calling the function set().

value words = set { "hello", "world" };
value greetings = set { "hello", "goodbye", "hola", "adios" };

Sets may be the subject of the binary union, intersection, and complement operators |, &, and ~.

value greetingsInWords = words & greetings;
value allWords = words | greetings;

An implementation of Set may compare elements for equality using Object.equals() or Comparable.compare().

See also set()

no type hierarchy

Attributes
distinctshared actual {Element*} distinct

A stream that produces every element produced by this stream exactly once. Duplicate elements of this stream are eliminated. Two elements are considered distinct unless they are both null, or unless they are both non-null and equal.

For example:

String("hello world".distinct)

is the string "helo wrd".

This is a lazy operation and the resulting stream reflects changes to this stream.

hashshared actual default Integer hash

The hash value of the value, which allows the value to be an element of a hash-based set or key of a hash-based map. Implementations must respect the constraint that:

  • if x==y then x.hash==y.hash.

Therefore, a class which refines Object.equals() must also refine hash.

Because the Integer type is platform-dependent a compiler for a given platform is permitted to further manipulate the calculated hash for an object, and the resulting hash may differ between platforms.

Refines Object.hash
Inherited Attributes
Attributes inherited from: Object
Attributes inherited from: Collection<Element>
Attributes inherited from: Iterable<Element,Absent>
Methods
cloneshared formal Set<Element> clone()

A shallow copy of this set, that is, a set with the same elements as this set, which do not change if the elements of this set change.

complementshared default Set<Element> complement<Other>(Set<Other> set)
given Other satisfies Object

Returns a new Set containing all the elements in this set that are not contained in the given Set.

containsshared actual default Boolean contains(Object element)

The fundamental operation for Sets. Determines if the given value belongs to this set.

Refines Collection.contains ultimately refines Category.contains
defaultNullElementsshared actual {Element*} defaultNullElements<Default>(Default defaultValue)
given Default satisfies Object

Produces a stream containing the elements of this stream, in the order in which they occur in this stream, after replacing every null element in the stream with the given default value. The value null does not ocur in the resulting stream.

For example, the expression

{ "123", "abc", "456" }.map(parseInteger).defaultNullElements(0)

results in the stream { 123, 0, 456 }.

equalsshared actual default Boolean equals(Object that)

Two Sets are considered equal if they have the same size and if every element of the first set is also an element of the second set, as determined by contains(). Equivalently, a set is equal to a second set if it is both a subset and a superset of the second set.

exclusiveUnionshared default Set<Element|Other> exclusiveUnion<Other>(Set<Other> set)
given Other satisfies Object

Returns a new Set containing only the elements contained in either this set or the given Set, but no element contained in both sets.

intersectionshared default Set<Element&Other> intersection<Other>(Set<Other> set)
given Other satisfies Object

Returns a new Set containing only the elements that are present in both this set and the given Set and that are instances of the intersection Element&Other of the element types of the two sets.

Note that, according to this definition, and even though 1==1.0 evaluates to true, the expression

set { 1 } & set { 1.0 }

produces the empty set {}.

subsetshared default Boolean subset(Set<Object> set)

Determines if this set is a subset of the given Set, that is, if the given set contains all of the elements in this set.

supersetshared default Boolean superset(Set<Object> set)

Determines if this set is a superset of the given Set, that is, if this set contains all of the elements in the given set.

unionshared default Set<Element|Other> union<Other>(Set<Other> set)
given Other satisfies Object

Returns a new Set containing all the elements of this set and all the elements of the given Set.

Note that it is possible for two sets of disjoint element type to be considered to have elements in common. For example, since 1==1.0 evaluates to true, the expression

set { 1 } | set { 1.0 }

produces the set { 1 }.

Inherited Methods
Methods inherited from: Object
Methods inherited from: Category<Element>
Methods inherited from: Collection<Element>
Methods inherited from: Iterable<Element,Absent>