Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between toString and mkString in scala?

I have a file that contains 10 lines - I want to retrieve it, and then split them with a newline("\n") delimiter.

here's what I did

val data = io.Source.fromFile("file.txt").toString; 

But this causes an error when I try to split the file on newlines.

I then tried

val data = io.Source.fromFile("file.txt").mkString; 

And it worked.

What the heck? Can someone tell me what the difference between the two methods are?

like image 972
alan Avatar asked Mar 17 '12 04:03

alan


People also ask

What does mkString do in Scala?

The mkString() method is utilized to display all the elements of the list in a string along with a separator. Return Type: It returns all the elements of the list in a string along with a separator.

What is toString in Scala?

The toString() method is utilized to return the string representation of the specified value. Method Definition: def toString(): String. Return Type: It returns the string representation of the specified value. Example #1: // Scala program of Int toString()


2 Answers

Let's look at the types, shall we?

scala> import scala.io._ import scala.io._  scala> val foo = Source.fromFile("foo.txt") foo: scala.io.BufferedSource = non-empty iterator  scala>  

Now, the variable that you have read the file foo.txt into is an iterator. If you perform toString() invocation on it, it doesn't return the contents of the file, rather the String representation of the iterator you've created. OTOH, mkString() reads the iterator(that is, iterates over it) and constructs a long String based on the values read from it.

For more info, look at this console session:

scala> foo.toString res4: java.lang.String = non-empty iterator  scala> res4.foreach(print) non-empty iterator scala> foo.mkString res6: String =  "foo bar baz quux dooo "  scala>  
like image 52
S.R.I Avatar answered Sep 22 '22 10:09

S.R.I


The toString method is supposed to return the string representation of an object. It is often overridden to provide a meaningful representation. The mkString method is defined on collections and is a method which joins the elements of the collection with the provided string. For instance, try something like:

val a = List("a", "b", "c") println(a.mkString(" : ")) 

and you will get "a : b : c" as the output. The mkString method has created a string from your collection by joining the elements of the collection with the string you provided. In the particular case you posted, the mkString call joined the elements returned by the BufferedSource iterator with the empty string (this is because you called mkString with no arguments). This results in simply concatenating all of the strings (yielded by the BufferedSource iterator) in the collection together.

On the other hand, calling toString here doesn't really make sense, as what you are getting (when you don't get an error) is the string representation of the BufferedSource iterator; which just tells you that the iterator is non-empty.

like image 42
nomad Avatar answered Sep 26 '22 10:09

nomad