Ignore spaces when reading from a file using get lines in Scala -


i trying read inputs file , counts them using map.i want ignore spaces when reading file.

val lines = source.fromfile("file path","utf-8").getlines()  val counts = new collection.mutable.hashmap[string, int].withdefaultvalue(0) lines.flatmap(line => line.split(" ")).foreach(word => counts(word) += 1) ((key, value) <- counts) println (key + "-->" + value) 

when try code following input.

hello hello     world goodbye hello   world 

the output is

world-->2 goodbye-->1 hello-->3 -->2 

it counts 2 spaces. how can fix ?

lines.flatmap(_.trim.split("\\s+")) 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -