Both can store mixed types but a Set seems far more powerful since it features union, intersection or diff among other.
Another important difference is that tuples aren't considered collections.
I'm learning Scala and I'm wondering why would I want to use tuples over sets.
The main advantage of tuples is that type information is maintained. This is a big deal in a statically typed language like Scala.
scala> (4.2,'c',true,"blah",7)
res2: (Double, Char, Boolean, String, Int) = (4.2,c,true,blah,7)
scala> Set(4.2,'c',true,"blah",7)
<console>:11: warning: a type was inferred to be `Any`; this may indicate a programming error.
Set(4.2,'c',true,"blah",7)
^
res3: scala.collection.immutable.Set[Any] = Set(true, blah, 7, c, 4.2)
Once our Set
type is Set[Any]
then we've lost type information that could be valuable in avoiding problems (ie bugs) down the line.
Also, pulling elements from a Set[Any]
can be a pain. Every element extracted from it [eg mySet.head
] will probably have to be tested for type before it can be used. Getting individual elements from a tuple can also be rather cumbersome [ myTup._3
] but the compiler, and the rest of your code, knows exactly what it's getting.
Sets:
Tuples:
You wouldn't use something like a Set when you need a Tuple. Imagine writing a method but instead of returning just an Int, for example, you need to return two Ints, or an Int and a String. Returning a Set[Any] doesn't really make sense there, but returning an (Int, Int) or (Int, String) is a lot more readable and clear in terms of intent.
A Set is iterable collection that can be either mutable or immutable and can only take distinct values.
val set = Set('a','b','b')
println(set)
set.foreach(i => print(i + ", "))
Will print
Set(a,b)
a, b,
A Tuple is immutable, and it is not a collection, and it will not reduce elements to distinct values and it maintains the types of the elements. It can be iterable if you apply the productIterator() method to the tuple.
val tuple = ('a','b','b')
println(tuple)
tuple.productIterator.foreach(i => println(i))
Will print
(a,b,b)
a, b, b,
A tuple is limited by 22 elements where a Set does not have that limit.
val set = Set('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')
println("set: " + set)
Will print set: Set(e, s, x, n, j, y, t, u, f, a, m, i, v, q, b, g, l, p, c, h, r, w, k, o, z, d)
A Set is arguably more robust while a tuple provides some surface level flexibility that a Set (and other collections) don't provide. Here is a graphic of some scala collections other than Set.
One huge difference between a set and a tuple, is that a set is viewed as a collection as if we took it out of Set Theory . A Set
can be used for efficiently intersecting, unions, diffing and also viewing if another set is a subset/superset of the other. There is no meaning to a duplicate elements in a set, as there may always be a representation of an element.
A TupleN
simply represents a container of N elements, and doesn't posses any of the properties that such a Set
holds. Thus, you'd want to use it when you want to holds multiple items in a "bag", whereas a set would be used when you want to view multiple items as a group and operate on them accordingly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.