I'm reading Set
You use a set instead of an array when you need to test efficiently for membership and you aren’t concerned with the order of the elements in the collection, or when you need to ensure that each element appears only once in a collection.
Basically Set
ensures uniqueness, it has some methods and relies on Hashable
Use the contains(_:) method to test whether a set contains a specific element.
Use the subtracting(_:) method to create a new set with the elements of a set that are not also in another set or sequence
But 2 different objects can have the same hashValue
, like in this post Swift Hashable
Do not assume that two instances of a type with the same hash value are equal. Depending on how we compute the hash value we can get collisions where two different instances share the same hash value. The Hashable protocol only needs the reverse - two equal instances have the same hash value.
So what if 2 objects have the same hashValue
, and Set
only saves 1, then we have the problem?
A set is a collection of unique elements. By unique, we mean no two elements in a set can be equal. Unlike sets in C++, elements of a set in Swift are not arranged in any particular order. Internally, A set in Swift uses a hash table to store elements in the set.
Swift 4 sets are used to store distinct values of same types but they don't have definite ordering as arrays have. You can use sets instead of arrays if ordering of elements is not an issue, or if you want to ensure that there are no duplicate values. (sets allow only distinct values.)
Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations.
In Swift, an Equatable is a protocol that allows two objects to be compared using the == operator. The hashValue is used to compare two instances. To use the hashValue , we first have to conform (associate) the type (struct, class, etc) to Hashable property.
An object that conforms to Hashable
must also be Equatable
. Set
uses ==
to test for equality, it doesn't depend only on the hashValue
.
From Apple's documentation on Hashable:
Conforming to the Hashable Protocol
To use your own custom type in a set or as the key type of a dictionary, add Hashable conformance to your type by providing a hashValue property. The Hashable protocol inherits from the Equatable protocol, so you must also add an equal-to operator (==) function for your custom type.
The documentation goes on to say:
Set and dictionary performance depends on hash values that minimize collisions for their associated element and key types, respectively.
So, the hashValue
is just used a first test for uniqueness; if the hashValue
s match then Set
will use the more computationally expensive ==
to test for uniqueness.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With