In scala, why does toSet() method mix up the order of elements in a collection (ListBuffer)?
Which collection can I use to both secure uniqueness of each element and keep their original order?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because the set abstraction, being a subclass of the traversable, has no guarantees about the order of elements held within:
More precisely about why the elements get ‘mangled’: the
toSetmethod constructs a new set collection out of some existing collection. It uses the default set implementation for this new set collection. The default set implementation is based on a hash table. In a hash table, the order of elements is undefined.