Difference and conversion between Seq [Int] and List [Int] in scala?

When I enter Seq(1,2,3) in the REPL, it returns me List(1,2,3)

 scala> Seq(1,2,3) res8: Seq[Int] = List(1, 2, 3) 

Therefore, I thought that List(1,2,3) could be of type List[Int] . And I tried to specify the type of the variable that is assigned the value of Seq(1,2,3) , but unexpectedly REPL complains as follows:

 scala> val a:List[Int]=Seq(1,2,3) <console>:20: error: type mismatch; found : Seq[Int] required: List[Int] val a:List[Int]=Seq(1,2,3) 

Does anyone have any ideas on what Seq[Int] = List(1, 2, 3) means? Doesn't that mean that Seq(1,2,3) returns a list? What is the difference between Seq[Int] and List[Int] ? And how to convert between Seq and List ?

+5
source share
1 answer

Seq is a basic attribute (interface) for sequences and List is a concrete implementation of this interface.

Each instance of List already a Seq , so there is no need to convert anything. You can use the toSeq method, but I see no reason for this. To convert Seq to List , use the toList method.

+8
source

Source: https://habr.com/ru/post/1209055/


All Articles