Set<E> abstract class
This class is the public interface of a set. A set is a collection without duplicates.
abstract class Set<E> extends IterableBase<E> {
factory Set() => new HashSet<E>();
/**
* Creates a [Set] that contains all elements of [other].
*/
factory Set.from(Iterable<E> other) => new HashSet<E>.from(other);
/**
* Returns true if [value] is in the set.
*/
bool contains(E value);
/**
* Adds [value] into the set. The method has no effect if
* [value] was already in the set.
*/
void add(E value);
/**
* Adds all of [elements] to this Set.
*
* Equivalent to adding each element in [elements] using [add],
* but some collections may be able to optimize it.
*/
void addAll(Iterable<E> elements);
/**
* Removes [value] from the set. Returns true if [value] was
* in the set. Returns false otherwise. The method has no effect
* if [value] value was not in the set.
*/
bool remove(Object value);
/**
* Removes all of [elements] from this set.
*/
void removeAll(Iterable elements);
/**
* Removes all elements of this set that are not
* in [elements].
*/
void retainAll(Iterable elements);
/**
* Removes all elements of this set that satisfy [test].
*/
void removeWhere(bool test(E element));
/**
* Removes all elements of this set that fail to satisfy [test].
*/
void retainWhere(bool test(E element));
/**
* Returns true if this Set contains all the elements of [other].
*/
bool containsAll(Iterable<E> other);
/**
* Returns a new set which is the intersection between this set and [other].
*/
Set<E> intersection(Set<E> other);
/**
* Returns a new set which contains all the elements of this set and [other].
*/
Set<E> union(Set<E> other);
/**
* Returns a new set with the the elements of this that are not in [other].
*/
Set<E> difference(Set<E> other);
/**
* Removes all elements in the set.
*/
void clear();
}
Extends
IterableBase<E> > Set<E>
Subclasses
Constructors
factory Set() #
factory Set() => new HashSet<E>();
Properties
final E first #
Returns the first element.
If this is empty throws a StateError. Otherwise this method is
equivalent to this.elementAt(0)
E get first {
Iterator it = iterator;
if (!it.moveNext()) {
throw new StateError("No elements");
}
return it.current;
}
final bool isEmpty #
final E last #
E get last {
Iterator it = iterator;
if (!it.moveNext()) {
throw new StateError("No elements");
}
E result;
do {
result = it.current;
} while(it.moveNext());
return result;
}
final int length #
final E single #
Returns the single element in this.
If this is empty or has more than one element throws a StateError.
E get single {
Iterator it = iterator;
if (!it.moveNext()) throw new StateError("No elements");
E result = it.current;
if (it.moveNext()) throw new StateError("More than one element");
return result;
}
Methods
abstract void add(E value) #
Adds value into the set. The method has no effect if value was already in the set.
abstract void addAll(Iterable<E> elements) #
Adds all of elements to this Set.
Equivalent to adding each element in elements using add, but some collections may be able to optimize it.
bool any(bool f(E element)) #
abstract void clear() #
Removes all elements in the set.
abstract bool containsAll(Iterable<E> other) #
Returns true if this Set contains all the elements of other.
abstract Set<E> difference(Set<E> other) #
Returns a new set with the the elements of this that are not in other.
E elementAt(int index) #
Returns the indexth element.
If [this] [Iterable] has fewer than index elements throws a RangeError.
Note: if this does not have a deterministic iteration order then the
function may simply return any element without any iteration if there are
at least
index elements in this.
E elementAt(int index) {
if (index is! int || index < 0) throw new RangeError.value(index);
int remaining = index;
for (E element in this) {
if (remaining == 0) return element;
remaining--;
}
throw new RangeError.value(index);
}
bool every(bool f(E element)) #
Iterable expand(Iterable f(E element)) #
Expand each element of this Iterable into zero or more elements.
The resulting Iterable will run through the elements returned by f for each element of this, in order.
The returned Iterable is lazy, and will call f for each element of this every time it's iterated.
Iterable expand(Iterable f(E element)) => new ExpandIterable<E, dynamic>(this, f);
E firstWhere(bool test(E value), {E orElse()}) #
Returns the first element that satisfies the given predicate f.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null, a StateError is
thrown.
E firstWhere(bool test(E value), { E orElse() }) {
// TODO(floitsch): check that arguments are of correct type?
for (E element in this) {
if (test(element)) return element;
}
if (orElse != null) return orElse();
throw new StateError("No matching element");
}
dynamic fold(initialValue, combine(previousValue, E element)) #
Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.
Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.
Example of calculating the sum of an iterable:
iterable.fold(0, (prev, element) => prev + element);
dynamic fold(var initialValue,
dynamic combine(var previousValue, E element)) {
var value = initialValue;
for (E element in this) value = combine(value, element);
return value;
}
void forEach(void f(E element)) #
abstract Set<E> intersection(Set<E> other) #
Returns a new set which is the intersection between this set and other.
String join([String separator]) #
Converts each element to a String and concatenates the strings.
Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.
String join([String separator]) {
Iterator<E> iterator = this.iterator;
if (!iterator.moveNext()) return "";
StringBuffer buffer = new StringBuffer();
if (separator == null || separator == "") {
do {
buffer.write("${iterator.current}");
} while (iterator.moveNext());
} else {
buffer.write("${iterator.current}");
while (iterator.moveNext()) {
buffer.write(separator);
buffer.write("${iterator.current}");
}
}
return buffer.toString();
}
E lastWhere(bool test(E value), {E orElse()}) #
Returns the last element that satisfies the given predicate f.
If none matches, the result of invoking the
orElse function is
returned. By default, when
orElse is null, a StateError is
thrown.
E lastWhere(bool test(E value), {E orElse()}) {
// TODO(floitsch): check that arguments are of correct type?
E result = null;
bool foundMatching = false;
for (E element in this) {
if (test(element)) {
result = element;
foundMatching = true;
}
}
if (foundMatching) return result;
if (orElse != null) return orElse();
throw new StateError("No matching element");
}
Iterable map(f(E element)) #
Returns a lazy Iterable where each element e of this is replaced
by the result of f(e).
This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.
Iterable map(f(E element)) => new MappedIterable<E, dynamic>(this, f);
E reduce(E combine(E value, E element)) #
Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.
Example of calculating the sum of an iterable:
iterable.reduce((value, element) => value + element);
E reduce(E combine(E value, E element)) {
Iterator<E> iterator = this.iterator;
if (!iterator.moveNext()) {
throw new StateError("No elements");
}
E value = iterator.current;
while (iterator.moveNext()) {
value = combine(value, iterator.current);
}
return value;
}
abstract bool remove(Object value) #
Removes value from the set. Returns true if value was in the set. Returns false otherwise. The method has no effect if value value was not in the set.
abstract void removeWhere(bool test(E element)) #
Removes all elements of this set that satisfy test.
abstract void retainAll(Iterable elements) #
Removes all elements of this set that are not in elements.
abstract void retainWhere(bool test(E element)) #
Removes all elements of this set that fail to satisfy test.
E singleWhere(bool test(E value)) #
Returns the single element that satisfies f. If no or more than one
element match then a StateError is thrown.
E singleWhere(bool test(E value)) {
// TODO(floitsch): check that argument is of correct type?
E result = null;
bool foundMatching = false;
for (E element in this) {
if (test(element)) {
if (foundMatching) {
throw new StateError("More than one matching element");
}
result = element;
foundMatching = true;
}
}
if (foundMatching) return result;
throw new StateError("No matching element");
}
Iterable<E> skip(int n) #
Iterable<E> skipWhile(bool test(E value)) #
Returns an Iterable that skips elements while test is satisfied.
The filtering happens lazily. Every new Iterator of the returned
Iterable will iterate over all elements of this.
As long as the iterator's elements do not satisfy
test they are
discarded. Once an element satisfies the
test the iterator stops testing
and uses every element unconditionally.
Iterable<E> skipWhile(bool test(E value)) {
return new SkipWhileIterable<E>(this, test);
}
Iterable<E> take(int n) #
Iterable<E> takeWhile(bool test(E value)) #
Returns an Iterable that stops once test is not satisfied anymore.
The filtering happens lazily. Every new Iterator of the returned
Iterable will start iterating over the elements of this.
When the iterator encounters an element e that does not satisfy
test,
it discards e and moves into the finished state. That is, it will not
ask or provide any more elements.
Iterable<E> takeWhile(bool test(E value)) {
return new TakeWhileIterable<E>(this, test);
}
List<E> toList({bool growable: true}) #
Set<E> toSet() #
abstract Set<E> union(Set<E> other) #
Returns a new set which contains all the elements of this set and other.
Iterable<E> where(bool f(E element)) #
Returns a lazy Iterable with all elements that satisfy the predicate f.
This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. Iterating will not cache results, and thus iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.
Iterable<E> where(bool f(E element)) => new WhereIterable<E>(this, f);