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 the scala collection

2025-01-18 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 the scala collection", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the scala collection.

List

/ / string list val site: List [String] = List ("Runoob", "Google", "Baidu") / / Integer list val nums: List [Int] = List (1,2,3,4) / / empty list val empty: List [Nothing] = List () / / 2D list val dim: list [Nothing] = List (List (1,0,0), List (0,1,0), List (0,0,0) 1))

The two basic units of the construction list are Nil and::

Nil can also be represented as an empty list.

/ / string list val site = "Runoob": ("Google": ("Baidu":: Nil)) / / Integer list val nums = 1:: (2: (3:: (4:: Nil)) / / empty list val empty = Nil// 2D list val dim = (1:: (0:: (0:: Nil):: (0:: ( 1:: (0:: Nil)):: (0:: (0:: (1:: Nil):: Nil

The Scala list has three basic operations:

Head returns the first element of the list

Tail returns a list of elements other than the first element

IsEmpty returns true when the list is empty

Connection list

You can use:: operator or List.::: () method or List.concat () method to join two or more lists. Examples are as follows:

Object Test {def main (args: Array [String]) {val site1 = "Runoob":: ("Google": ("Baidu":: Nil)) val site2 = "Facebook":: ("Taobao":: Nil) / / use:: operator var fruit = site1: site2 println ("site1:: site2:" + fruit) / / use List.::: () method fruit = site1.::: (site2) println ("site1.::: (site2):" + fruit) / / use concat method fruit = List.concat (site1) Site2) println ("List.concat (site1, site2):" + fruit)}} List.fill ()

We can use the List.fill () method to create a list of elements with a specified number of repeats:

List.tabulate ()

The List.tabulate () method creates a list with a given function.

The first parameter of the method is the number of elements, which can be two-dimensional, and the second parameter is the specified function. We calculate the result through the specified function and insert the return value into the list with a starting value of 0.

List.reverse

List.reverse is used to reverse the order of the list

Map

/ / empty hash table, the key is a string and the value is the integer var A:Map [Char,Int] = Map () / / Map key-value pair val colors = Map ("red"-> "# FF0000", "azure"-> "# F0FFFF") needs to define the type for the key-value pair. If you need to add key-value pairs, you can use the + sign, as follows: a + = ('I'- > 1) A + = ('J'-> 5) A + = ('K'-> 10) A + = ('L'-> 100Map basic operation keys returns all keys of Map (key) values returns all values of Map (value) isEmpty returns trueMap merge when Map is empty

You can use the + operator or the Map.++ () method to concatenate two Map,Map that will remove duplicate key when merged.

At this point, I believe you have a deeper understanding of "how to use scala collections". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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