How to use Scala to realize worldcount function
This article mainly explains "how to use Scala to achieve worldcount function", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use Scala to achieve worldcount function" bar!
Create a new ScalaWorldCount object:
Package com.ruozedata.day4
Object ScalaWorldCount {
Def main (args: Array [String]): Unit = {
Val lines = List ("hello ruoze hello jepson", "hello ruoze hello jepson hello china")
/ * * the first method
* flatMap=map+flatten lines.flatMap flattens data
* flatMap (_ .split ("")) takes each element (string) and cuts it according to the space
* map ((_, 1)) put the word occurrence once and 1 together (into tuples)
* the first _ of groupBy (_. _ 1) represents each tuple in List. A certain element in the tuple is taken with. _ n, that is, grouped according to an element in the tuple, and a map is returned.
The first _ gets a dual tuple and fetches the first tuple in the tuple. * map (t = > (t = > (t = 1). _. _ 2.size is the number of the second dual tuple.
*
, /
Val wc = lines.flatMap (_ .split (")). Map ((_, 1)). GroupBy (_. _ 1) .map (t = > (t = > (t.split _ 1 ~ 2.size)). ToList / / .sortBy (_. _ 2). Reverse
For (j)