scala.collection

trait SetLike

[source: scala/collection/SetLike.scala]

trait SetLike[A, +This <: SetLike[A, This] with Set[A]]
extends IterableLike[A, This] with Addable[A, This] with Subtractable[A, This]

A generic template for sets of type A.
To implement a concrete set, you need to provide implementations of the following methods (where This is the type of the set in question):

    def contains(key: A): Boolean
    def iterator: Iterator[A]
    def +(elem: A): This
    def -(elem: A): This

If you wish that methods like, take, drop, filter return the same kind of set, you should also override:

   def empty: This

It is also good idea to override methods foreach and size for efficiency.

Author
Martin Odersky
Version
2.8
Direct Known Subclasses:
Enumeration.ValueSet, BitSetLike, Set, SetProxyLike, SortedSetLike, HashSet, ListSet, Set, SetLike

Method Summary
def & (that : Set[A]) : This
Returns a new set consisting of all elements that are both in the current set and in the argument set.
def &~ (that : Set[A]) : This
The difference of this set and the given set that.
def ** (that : Set[A]) : This
This method is an alias for intersect. It computes an intersection with set that. It removes all the elements that are not present in that.
abstract def + (elem : A) : This
Creates a new set with an additional element, unless the element is already present.
abstract def - (elem : A) : This
Creates a new set with given element removed from this set, unless the element is not present.
def apply (elem : A) : Boolean
This method allows sets to be interpreted as predicates. It returns true, iff this set contains element elem.
abstract def contains (elem : A) : Boolean
Checks if this set contains element elem.
def diff (that : Set[A]) : This
The difference of this set and the given set that.
abstract def empty : This
override def equals (that : Any) : Boolean
Compares this set with another object and returns true, iff the other object is also a set which contains the same elements as this set.
override def hashCode : Int
Returns a hash code value for the object.
def intersect (that : Set[A]) : This
Returns a new set consisting of all elements that are both in the current set and in the argument set.
override def isEmpty : Boolean
Checks if this set is empty.
protected[this] override def newBuilder : Builder[A, This]
A common implementation of newBuilder for all sets in terms of empty. Overridden for mutable sets in mutable.SetLike.
override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation.
def subsetOf (that : Set[A]) : Boolean
Checks if this set is a subset of set that.
override def toString : java.lang.String
Need to override string, so that it's not the Function1's string that gets mixed in.
def union (that : Set[A]) : This
The union of this set and the given set that.
def | (that : Set[A]) : This
The union of this set and the given set that.
Methods inherited from Subtractable
-, --, --
Methods inherited from Addable
+, ++, ++
Methods inherited from IterableLike
iterator (abstract), thisCollection, toCollection, elements, foreach, forall, exists, find, foldRight, reduceRight, toIterable, head, take, slice, takeWhile, takeRight, dropRight, copyToArray, zip, zipAll, zipWithIndex, sameElements, toStream, canEqual, view, view, first, firstOption, projection
Methods inherited from TraversableLike
repr, nonEmpty, size, hasDefiniteSize, ++, ++, map, flatMap, filter, filterNot, partialMap, remove, partition, groupBy, count, foldLeft, /:, :\, reduceLeft, reduceLeftOption, reduceRightOption, sum, product, min, max, headOption, tail, last, lastOption, init, drop, dropWhile, span, splitAt, copyToBuffer, copyToArray, toArray, toList, toSeq, toIndexedSeq, toSet, mkString, mkString, mkString, addString, addString, addString, withFilter
Methods inherited from AnyRef
getClass, clone, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
abstract def empty : This

protected[this] override def newBuilder : Builder[A, This]
A common implementation of newBuilder for all sets in terms of empty. Overridden for mutable sets in mutable.SetLike.

abstract def contains(elem : A) : Boolean
Checks if this set contains element elem.
Parameters
elem - the element to check for membership.
Returns
true iff elem is contained in this set.

abstract def +(elem : A) : This
Creates a new set with an additional element, unless the element is already present.
Parameters
elem - the element to be added
Overrides
Addable.+

abstract def -(elem : A) : This
Creates a new set with given element removed from this set, unless the element is not present.
Parameters
elem - the element to be removed
Overrides
Subtractable.-

override def isEmpty : Boolean
Checks if this set is empty.
Returns
true iff there is no element in the set.
Overrides
IterableLike.isEmpty

def apply(elem : A) : Boolean
This method allows sets to be interpreted as predicates. It returns true, iff this set contains element elem.
Parameters
elem - the element to check for membership.
Returns
true iff elem is contained in this set.

def intersect(that : Set[A]) : This
Returns a new set consisting of all elements that are both in the current set and in the argument set.
Parameters
that - the set to intersect with.

def &(that : Set[A]) : This
Returns a new set consisting of all elements that are both in the current set and in the argument set.
Parameters
that - the set to intersect with.
Notes
same as intersect.

@deprecated("use & instead")

def **(that : Set[A]) : This
This method is an alias for intersect. It computes an intersection with set that. It removes all the elements that are not present in that.
Parameters
that - the set to intersect with

def union(that : Set[A]) : This
The union of this set and the given set that.
Parameters
that - the set of elements to add
Returns
a set containing the elements of this set and those of the given set that.

def |(that : Set[A]) : This
The union of this set and the given set that.
Parameters
that - the set of elements to add
Returns
a set containing the elements of this set and those of the given set that.
Notes
same as union.

def diff(that : Set[A]) : This
The difference of this set and the given set that.
Parameters
that - the set of elements to remove
Returns
a set containing those elements of this set that are not also contained in the given set that.

def &~(that : Set[A]) : This
The difference of this set and the given set that.
Parameters
that - the set of elements to remove
Returns
a set containing those elements of this set that are not also contained in the given set that.
Notes
same as diff.

def subsetOf(that : Set[A]) : Boolean
Checks if this set is a subset of set that.
Parameters
that - another set.
Returns
true iff the other set is a superset of this set. todo: rename to isSubsetOf

override def stringPrefix : java.lang.String
Defines the prefix of this object's toString representation.

override def toString : java.lang.String
Need to override string, so that it's not the Function1's string that gets mixed in.

override def hashCode : Int
Returns a hash code value for the object.

The default hashing algorithm is platform dependent. Note that it is allowed for two objects to have identical hash codes (o1.hashCode.equals(o2.hashCode)) yet not be equal (o1.equals(o2) returns false). A degenerate implementation could always return 0. However, it is required that if two objects are equal (o1.equals(o2) returns true) that they have identical hash codes (o1.hashCode.equals(o2.hashCode)). Therefore, when overriding this method, be sure to verify that the behavior is consistent with the equals method.

Returns
the hash code value for the object.


override def equals(that : Any) : Boolean
Compares this set with another object and returns true, iff the other object is also a set which contains the same elements as this set.
Parameters
that - the other object
Notes
not necessarily run-time type safe.
Returns
true iff this set and the other set contain the same elements.