HashSetImplementation<E> class
class HashSetImplementation<E > implements HashSet<E> { HashSetImplementation() { _backingMap = new HashMapImplementation<E, E>(); } factory HashSetImplementation.from(Iterable<E> other) { Set<E> set = new HashSetImplementation<E>(); for (final e in other) { set.add(e); } return set; } void clear() { _backingMap.clear(); } void add(E value) { _backingMap[value] = value; } bool contains(E value) { return _backingMap.containsKey(value); } bool remove(E value) { if (!_backingMap.containsKey(value)) return false; _backingMap.remove(value); return true; } void addAll(Collection<E> collection) { collection.forEach(void _(E value) { add(value); }); } Set<E> intersection(Collection<E> collection) { Set<E> result = new Set<E>(); collection.forEach(void _(E value) { if (contains(value)) result.add(value); }); return result; } bool isSubsetOf(Collection<E> other) { return new Set<E>.from(other).containsAll(this); } void removeAll(Collection<E> collection) { collection.forEach(void _(E value) { remove(value); }); } bool containsAll(Collection<E> collection) { return collection.every(bool _(E value) { return contains(value); }); } void forEach(void f(E element)) { _backingMap.forEach(void _(E key, E value) { f(key); }); } Set map(f(E element)) { Set result = new Set(); _backingMap.forEach(void _(E key, E value) { result.add(f(key)); }); return result; } Dynamic reduce(Dynamic initialValue, Dynamic combine(Dynamic previousValue, E element)) { return Collections.reduce(this, initialValue, combine); } Set<E> filter(bool f(E element)) { Set<E> result = new Set<E>(); _backingMap.forEach(void _(E key, E value) { if (f(key)) result.add(key); }); return result; } bool every(bool f(E element)) { Collection<E> keys = _backingMap.getKeys(); return keys.every(f); } bool some(bool f(E element)) { Collection<E> keys = _backingMap.getKeys(); return keys.some(f); } bool isEmpty() { return _backingMap.isEmpty(); } int get length { return _backingMap.length; } Iterator<E> iterator() { return new HashSetIterator<E>(this); } String toString() { return Collections.collectionToString(this); } // The map backing this set. The associations in this map are all // of the form element -> element. If a value is not in the map, // then it is not in the set. HashMapImplementation<E, E> _backingMap; }
Implements
Constructors
new HashSetImplementation() #
HashSetImplementation() { _backingMap = new HashMapImplementation<E, E>(); }
Properties
final int length #
Returns the number of elements in this collection.
int get length { return _backingMap.length; }
Methods
void addAll(Collection<E> collection) #
void clear() #
bool containsAll(Collection<E> collection) #
bool every(bool f(E element)) #
Returns true if every elements of this collection satisify the predicate f. Returns false otherwise.
bool every(bool f(E element)) { Collection<E> keys = _backingMap.getKeys(); return keys.every(f); }
Set<E> filter(bool f(E element)) #
Returns a collection with the elements of this collection that satisfy the predicate f.
The returned collection should be of the same type as the collection creating it.
An element satisfies the predicate
f if f(element)
returns true.
Set<E> filter(bool f(E element)) { Set<E> result = new Set<E>(); _backingMap.forEach(void _(E key, E value) { if (f(key)) result.add(key); }); return result; }
void forEach(void f(E element)) #
Applies the function f to each element of this collection.
void forEach(void f(E element)) { _backingMap.forEach(void _(E key, E value) { f(key); }); }
Set<E> intersection(Collection<E> collection) #
bool isEmpty() #
Returns true if there is no element in this collection.
bool isEmpty() { return _backingMap.isEmpty(); }
bool isSubsetOf(Collection<E> other) #
Set map(f(E element)) #
Returns a new collection with the elements f(e)
for each element e
of this collection.
Subclasses of Collection
should implement the map method
to return a collection of the same general type as themselves.
E.g., List.map
should return a List
.
Set map(f(E element)) { Set result = new Set(); _backingMap.forEach(void _(E key, E value) { result.add(f(key)); }); return result; }
reduce(initialValue, combine(previousValue, E element)) #
Reduce 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 a collection:
collection.reduce(0, (prev, element) => prev + element);
Dynamic reduce(Dynamic initialValue, Dynamic combine(Dynamic previousValue, E element)) { return Collections.reduce(this, initialValue, combine); }