I’ve seen in many examples that sometimes a Seq is being used, while other times is the List…
Is there any difference, other than the former one being a Scala type and the List coming from Java?
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.
In Java terms, Scala’s
Seqwould be Java’sList, and Scala’sListwould be Java’sLinkedList.Note that
Seqis atrait, which is similar to Java’sinterface, but with the equivalent of up-and-coming defender methods. Scala’sListis an abstract class that is extended byNiland::, which are the concrete implementations ofList.So, where Java’s
Listis aninterface, Scala’sListis an implementation.Beyond that, Scala’s
Listis immutable, which is not the case ofLinkedList. In fact, Java has no equivalent to immutable collections (the read only thing only guarantees the new object cannot be changed, but you still can change the old one, and, therefore, the "read only" one).Scala’s
Listis highly optimized by compiler and libraries, and it’s a fundamental data type in functional programming. However, it has limitations and it’s inadequate for parallel programming. These days,Vectoris a better choice thanList, but habit is hard to break.Seqis a good generalization for sequences, so if you program to interfaces, you should use that. Note that there are actually three of them:collection.Seq,collection.mutable.Seqandcollection.immutable.Seq, and it is the latter one that is the "default" imported into scope.There’s also
GenSeqandParSeq. The latter methods run in parallel where possible, while the former is parent to bothSeqandParSeq, being a suitable generalization for when there is no concern for code parallelism. They are both relatively new, so people don’t use them as much.