Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Scala to realize worldcount function

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report