scala data collection

Catalog

Data Collection Type

Invariant Array

Variable Array

Invariant List

Variable List

Invariant Set

Variable Set

Invariant Map

Variable Map

tuple

Data Collection Type

scala's data collection types are mainly Array, List, Set, Map, Tuple, etc. They are divided into variable and immutable collections, which can be updated and expanded. Inmutable collections can also be updated and expanded, but immutable collections can be changed by creating a new collection.

Invariant Array

Arrays are ordered, repeatable, and can store data collections of different data types.

Several ways to create arrays:

val array1:Array[Int] = new Array[Int](5)//Basic Format, Determine Array Length
val array2 = Array(1,2,3,4,5)//Quick Creation
val array3 = Array.ofDim[Int](3, 3)//Creating a multidimensional 3*3 array using ofDim

Several ways to traverse arrays

for (i <- array) {println(s"${i}.hello")}//for traversing arrays directly
for (i <- 0 until array.length) {println(s"${i}.hello")}//Traverse by Length
//Yellow appears in Idea and can be traversed with the following index
for (i <- array.indices) {println(s"${i}.hello")}//for traversal with indices, index traversal

Array Addendum Check

array(2)//Find Array 3rd
array(2)=8//assignment
val array1 = 1+: array :+2//Add 1 to the first array, 2 to the last, and pass it to array1. Because an immutable array must be passed again, the array itself does not change

Invariant arrays cannot be printed directly

val array2 = Array(1,2,3,4,5)
println(array2)//This prints the memory address of the array

Variable Array

First import the package of the variable array to create the variable array:

import scala.collection.mutable.ArrayBuffer
val array:ArrayBuffer[Int] = ArrayBuffer[Int](5)

Array add-delete check

array.+= (2)//Append 2 at the end
array.append(4)//Append 4 at the end
array.insert(1,2,3)//Insert 2,3 at the subscript position of 1
array(1)=2//Update subscript position of 1 to 2
array.remove(3)//Delete the fourth element

Variable arrays can be printed directly

val array = ArrayBuffer(1,2,3,4,5)
println(array)//Output ArrayBuffer(1, 2, 3, 4, 5)

Note: It is best not to assign two variable arrays, which will change one of the arrays and the other later. The name of the variable data actually points to the storage space of the array, so that the two are equal, so that they point to the same space and the values are the same.

Variable arrays in use recommend using keywords (calling functions) to make changes to the array, and non-variable arrays recommend using identifiers (:+2) to make changes.

Invariant List

An immutable list is a collection of data that is sequential but has no sequence, can store duplicate data, and can store different data.

Create List

val list1:List[Int] =  List[Int](5)//Create an empty list of length 5
val list2 = List(1,2,3,4,5)

List Operation

val list2 = 1 :: 2 :: 3 :: 4 :: Nil//Append data to an empty List
val list3 = list2.+:(5)//Append a 5 to the end of list2
val list3 = list2 ::: list1//Merge list2,1
list3.foreach(println)//Traversal Output
println(list3(2))//The third output of list3, the bottom of the list method is the third traversal output.

Variable List

Create List

    val list0:ListBuffer[Int] = ListBuffer[Int](5)//Create an empty list of length 5
    val list = ListBuffer(1,2,3,4)

List Operation

    list.append(5)//Append 5
    list.insert(1,2)//Insert 2 at 1 position
    list(1)=4//Modify direct assignment
    list.update(1,5)//modify
    list.remove(0)//Delete data

Invariant Set

The data is out of order and not repeatable. If duplicate data is created, the final output will be one.

    val set0:Set[Int] = Set[Int](5)//Create an empty Set of length 5
    val set = Set(1,2,3,4,5)
    for (i <- set)(
      println(i)
    )//Traversal output, out-of-order output

Variable Set

    val set = mutable.Set(1,2,3,4,5,3)//Create Set
    set += 7//Add Elements
    set -= 5//Delete element
    val set2 =set.+(9)//Assign values and add elements
    for (i <- set2)(
      println(i)
    )//Create Set
  }

Invariant Map

Is a Hash list that stores key-value pairs.

    val map = Map("a" -> 12,"b" -> 42,"c" -> 52)//Create Map
    println(map)
    map.foreach(println)//Traversal Printing
    println(map.get("a"))//Return current value
    println(map.getOrElse("d" , 0))//If there is no K=d, V=0
    println(map("a"))//Output K=a of v

Variable Map

    val map1 = mutable.Map("a" -> 13,"b" -> 43,"c" -> 53)
    val map2 = mutable.Map("q" -> 13,"w" -> 43,"e" -> 53)
    map1.put("d" ,45)//Add Elements
    map1.update("d" ,34)//Change the v for k=d to 34
    map1.remove("c")//Delete element
    map1 ++= map2//Add or modify values

tuple

A tuple is a collection that can hold different data types, encapsulating multiple unrelated data as a whole.

    val tuple1 = (1,"qwe",23)//Create tuples
    println(tuple1)//Output tuple
    println(tuple1._1)//First value of output tuple
    for (i <- tuple1.productIterator) {
      println(i)
    }//Traversal tuples

Keywords: Scala

Added by tmed on Thu, 30 Sep 2021 19:22:21 +0300