35th: List of map, FlatMap, foreach, filter Operation code Combat

Source: Internet
Author: User

The role of the list's map function:

The map parameter is a function, and each element in the list is applied to the function, and a new collection is returned.

For example we have a list (1,2,3,4) and we need to build list (2,4,6,8)

Then we can use the map function to pass in a * * function

As follows:

scala> val list = list (1,2,3,4) list:list[int] = List (1, 2, 3, 4) scala> list.map (x = x*2) Res0:list[int] = list ( 2, 4, 6, 8)

x = x*2 is an anonymous function. Each element in the list is applied to this function.

This is an anonymous function with only one parameter, and you can use _ placeholders to simplify the notation.

Scala> List.map (_*2) res1:list[int] = List (2, 4, 6, 8)
Scala> Val bigdata =list ("Spark", "Hadoop", "HBase") bigdata:list[string] = List (Spark, Hadoop, HBase) scala> Bigdata.map (_.length) Res2:list[int] = List (5, 6, 5)
Scala> Bigdata.map (_.tolist) Res3:list[list[char]] = List (list (s, p, a, R, K), List (H, a, D, O, O, p), list (H, B, a, S, e))

We'll print every word in the bigdata in a flashback.

Scala> Bigdata.map (_.tolist.reverse.mkstring) res6:list[string] = List (Kraps, Poodah, Esabh)


Flatmap function:

The "FlatMap" function has half the same function as the map function, but there is a requirement that the returned value of the incoming function be a list (should be seq) after processing, and if the result is not a list (seq), an error occurs. In other words, the function passed in is required-the return value is a SEQ line. Thus, after each element is processed, a list is returned, and we get a list,flatmap that contains a list element that automatically pulls all the elements of the internal list to form a list return.

Scala> List.flatmap (_*2) <console>:12:error:type mismatch;       Found:int required:scala.collection.gentraversableonce[?] List.flatmap (_*2)

Because _*2 return is not seq, so the error.

Scala> List.flatmap (_ match {case x = = List (x*2)}) Res5:list[int] = List (2, 4, 6, 8)


The foreach function:

The parameter is also a function that applies this parameter function to each element in the list.

scala> var sum =0sum:int = 0scala> list.foreach (sum +=_) scala> println (sum) 10


Filter function:

Returns a list whose arguments are a function that returns a Boolean type, or returns a value of True if the return.

If we want to keep the even numbers in the list, we can use the following methods:

Scala> List.filter {x = x%2 = = 0}res9:list[int] = List (2, 4)


This article is from the "Ding Dong" blog, please be sure to keep this source http://lqding.blog.51cto.com/9123978/1742034

35th: List of map, FlatMap, foreach, filter Operation code Combat

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.