1. A tuple
Tuples can be used to contain a set of values of different types. For example: name, age, sex, date of birth. The elements of a tuple are immutable.
1.1 Defining tuples
grammar
1, use parentheses to define tuplesval/varTuples = (elements1Elements,2Elements,3....).2, use arrows to define tuples (tuples have only two elements)val/varTuples = elements1- > element2
Copy the code
The sample
// Define a student tuple using parentheses
scala> val s = (1."zhangsan".20."beijing")
s: (Int.String.Int.String) = (1,zhangsan,20,beijing)
// Use arrows to define tuples (tuples have only two elements)
scala> val a = "zhangsan" -> 20
a: (String.Int) = (zhangsan,20)
Copy the code
1.2 Accessing tuples
Use _1, _2, _3…. To access elements in a tuple, _1 means access to the first element, and so on
The sample
scala> val a = "zhangsan" -> "male"
a: (String.String) = (zhangsan,male)
// Get the first element
scala> a._1
res41: String = zhangsan
// Get the second element
scala> a._2
res42: String = male
Copy the code
List 2.
Lists are the most important and commonly used data structure in Scala. List has the following properties:
- Duplicate values can be saved
- There’s a sequence
In Scala, there are also two types of lists, immutable and mutable
2.1 Immutable list
grammar
Use List(element 1, element 2, element 3…) To create an immutable list (the elements and lengths of the list are immutable)
val/varThe variable name =List(element1Elements,2Elements,3...)
Copy the code
Use Nil to create an immutable empty list
val/varThe variable name =Nil
Copy the code
Create an immutable list using the :: method
// To create a list using **::** concatenation, you must add **Nil** at the end
val/varVariable name = element1Elements: :2: :Nil
Copy the code
The sample
scala> val a = List(1.2.3.4)
a: List[Int] = List(1.2.3.4)
// Use Nil to create an immutable empty list
scala> val a = Nil
a: scala.collection.immutable.Nil.type = List(a)// Use the '::' method to create a list of elements -2 and -1
scala> val a = 2 -: :- 1: :Nil
a: List[Int] = List(2 -.- 1)
Copy the code
2.2 Mutable list
A mutable list is a list whose elements and lengths are mutable.
To use the variable list, first to import the import scala. Collection. The mutable. ListBuffer
define
Create an empty mutable list using ListBuffer[element type]().
val/varThe variable name =ListBuffer[Int] ()Copy the code
Use the ListBuffer(element 1, element 2, element 3…) Create a mutable list.
val/varThe variable name =ListBuffer(element1Elements,2Elements,3...)
Copy the code
The sample
// Create an empty integer mutable list
scala> val a = ListBuffer[Int]()
a: scala.collection.mutable.ListBuffer[Int] = ListBuffer(a)// create a mutable list containing the following elements: 1,2,3,4
scala> val list = ListBuffer(1.2.3.4)
list: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1.2.3.4)
Copy the code
2.3 Mutable list operations
- Gets the element (accessed using parentheses
(Index value)
) - Add elements (
+ =
) - Append a list (
+ + =
) - Change elements (
Use parentheses to get the element and then assign it
) - Delete elements (
- =
) - Convert to List (
toList
) - Convert to Array (
toArray
)
The sample
Reference code
// Import the mutable list
scala> import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
// Create a mutable list
scala> val a = ListBuffer(1.2.3)
a: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1.2.3)
// Get the first element
scala> a(0)
res19: Int = 1
// Appends an element
scala> a += 4
res20: a.type = ListBuffer(1.2.3.4)
// Append a list
scala> a ++= List(5.6.7)
res21: a.type = ListBuffer(1.2.3.4.5.6.7)
// Delete elements
scala> a -= 7
res22: a.type = ListBuffer(1.2.3.4.5.6)
But A itself is still a mutable list. It just generates a new list
scala> a.toList
res23: List[Int] = List(1.2.3.4.5.6)
But a itself is still a mutable list, just generating a new array
scala> a.toArray
res24: Array[Int] = Array(1.2.3.4.5.6)
Copy the code
2.4 Common List Operations
The following are common operations for lists
- Determine if the list is empty (
isEmpty
) - Concatenate two lists (
++
) - Gets the first element of the list (
head
) and the remainder (tail
) - Reverse the list (
reverse
) - Get prefix (
take
), get the suffix (drop
) - Flattening (
flaten
) - Zipper (
zip
) and pull apart (unzip
) - Conversion string (
toString
) - Generate string (
mkString
) - And set (
union
) - Intersection (
intersect
) - Difference set (
diff
)
scala> val a = List(1.2.3)
a: List[Int] = List(1.2.3)
// 1. Check whether the list is empty
scala> a.isEmpty
res51: Boolean = false
// 2
scala> val b = List(4.5.6)
b: List[Int] = List(4.5.6)
scala> a ++ b
res52: List[Int] = List(1.2.3.4.5.6)
// get the first element and the rest of the list
scala> a.head
res4: Int = 1
scala> a.tail
res5: List[Int] = List(2.3)
// reverse the list
scala> a.reverse
res6: List[Int] = List(3.2.1)
// get the list prefix and suffix
scala> a.take(2)
res56: List[Int] = List(1.2)
scala> a.drop(2)
res60: List[Int] = List(3)
// Flattening (flattening) Flattening means putting all the elements of a list within a list into one list.
scala> val a = List(List(1.2), List(3), List(4.5))
a: List[List[Int]] = List(List(1.2), List(3), List(4.5))
scala> a.flatten
res0: List[Int] = List(1.2.3.4.5)
// 7, zip and unzip
// Zip: use zip to combine two lists into a tuple list
scala> val a = List("zhangsan"."lisi"."wangwu")
a: List[String] = List(zhangsan, lisi, wangwu)
scala> val b = List(19.20.21)
b: List[Int] = List(19.20.21)
scala> a.zip(b)
res1: List[(String.Int)] = List((zhangsan,19), (lisi,20), (wangwu,21))
// Unpack: Unpack a list containing tuples into a tuple containing two lists
scala> res1.unzip
res2: (List[String].List[Int]) = (List(zhangsan, lisi, wangwu),List(19.20.21))
The toString method returns all elements of a List
scala> val a = List(1.2.3.4)
a: List[Int] = List(1.2.3.4)
scala> println(a.toString)
List(1.2.3.4)
// 9. Generate string to concatenate elements with delimiters. There are no delimiters by default
scala> val a = List(1.2.3.4)
a: List[Int] = List(1.2.3.4)
scala> a.mkString
res7: String = 1234
scala> a.mkString(":")
res8: String = 1:2:3:4
// select * from list
scala> val a1 = List(1.2.3.4)
a1: List[Int] = List(1.2.3.4)
scala> val a2 = List(3.4.5.6)
a2: List[Int] = List(3.4.5.6)
// Merge operation
scala> a1.union(a2)
res17: List[Int] = List(1.2.3.4.3.4.5.6)
// Call distinct
scala> a1.union(a2).distinct
res18: List[Int] = List(1.2.3.4.5.6)
// 11
scala> val a1 = List(1.2.3.4)
a1: List[Int] = List(1.2.3.4)
scala> val a2 = List(3.4.5.6)
a2: List[Int] = List(3.4.5.6)
scala> a1.intersect(a2)
res19: List[Int] = List(3.4)
Diff (a2), which means that a1 does not exist in A2
scala> val a1 = List(1.2.3.4)
a1: List[Int] = List(1.2.3.4)
scala> val a2 = List(3.4.5.6)
a2: List[Int] = List(3.4.5.6)
scala> a1.diff(a2)
res24: List[Int] = List(1.2)
Copy the code
3. Set
A Set is a Set that represents no repeating elements. Set has the following properties:
- Elements do not repeat
- Insertion order is not guaranteed
3.1 Immutable set
3.1.1 defines
grammar
Create an empty immutable set with the syntax:
val/varThe variable name =Set[type] ()Copy the code
Given an element to create an immutable set, the syntax is:
val/varThe variable name =Set(element1Elements,2Elements,3...)
Copy the code
The sample
// Define an empty immutable set
scala> val a = Set[Int]()
a: scala.collection.immutable.Set[Int] = Set(a)// define an immutable set that holds the following elements: 1,1,3,2,4,8
scala> val a = Set(1.1.3.2.4.8)
a: scala.collection.immutable.Set[Int] = Set(1.2.3.8.4)
Copy the code
3.1.2 Basic Operations
- Gets the size of the set (
size
) - Traverse the set (
Consistent with the traversal number group
) - Add an element and generate a Set (
+
) - Concatenate two sets to generate a Set (
++
) - Concatenate sets and lists to generate a Set (
++
)
Reference code
/ / create a set
scala> val a = Set(1.1.2.3.4.5)
a: scala.collection.immutable.Set[Int] = Set(5.1.2.3.4)
// Get the set size
scala> a.size
res0: Int = 5
/ / traverse the set
scala> for(i <- a) println(i)
// Deleting an element does not change a, but just generates another set
scala> a - 1
res5: scala.collection.immutable.Set[Int] = Set(5.2.3.4)
// Splice two sets
scala> a ++ Set(6.7.8)
res2: scala.collection.immutable.Set[Int] = Set(5.1.6.2.7.3.8.4)
// Concatenate sets and lists
scala> a ++ List(6.7.8.9)
res6: scala.collection.immutable.Set[Int] = Set(5.1.6.9.2.7.3.8.4)
Copy the code
3.2 variable set
define
Manual import, an import scala. Collection. The mutable. Set
scala> val a = Set(1.2.3.4)
a: scala.collection.mutable.Set[Int] = Set(1.2.3.4)
// Add elements
scala> a += 5
res25: a.type = Set(1.5.2.3.4)
// Delete elements
scala> a -= 1
res26: a.type = Set(5.2.3.4)
Copy the code
4. The mapping
A Map can be called a mapping. It’s a collection of key-value pairs. In Scala, maps are also divided into immutable maps and mutable maps.
4.1 Immutable Map
define
grammar
val/var map = Map(key -> value, key -> value, key -> value...)// It is recommended for better readability
val/var map = Map(key, value), (key, value), (key, value)...Copy the code
The sample
scala> val map = Map("zhangsan"->30."lisi"->40)
map: scala.collection.immutable.Map[String.Int] = Map(zhangsan -> 30, lisi -> 40)
scala> val map = Map(("zhangsan".30), ("lisi".30))
map: scala.collection.immutable.Map[String.Int] = Map(zhangsan -> 30, lisi -> 30)
// Get value based on key
scala> map("zhangsan")
res10: Int = 30
Copy the code
4.2 variable Map
define
Define syntax consistent with immutable Map. But define variable Map need to manually import import scala. Collection. The mutable. The Map
The sample
scala> val map = Map("zhangsan"->30."lisi"->40)
map: scala.collection.mutable.Map[String.Int] = Map(lisi -> 40, zhangsan -> 30)
/ / modify the value
scala> map("zhangsan") = 20
Copy the code
4.3 Basic Map Operations
Basic operation
- Get the value (
map(key)
) - Get all keys (
map.keys
) - Get all values (
map.values
) - Iterate over the Map collection
- getOrElse
- Increase the key, the value of
- Remove the key
The sample
scala> val map = Map("zhangsan"->30."lisi"->40)
map: scala.collection.mutable.Map[String.Int] = Map(lisi -> 40, zhangsan -> 30)
// Get zhagnsan's age
scala> map("zhangsan")
res10: Int = 30
// Get all student names
scala> map.keys
res13: 可迭代[String] = Set(lisi, zhangsan)
// Get all student ages
scala> map.values
res14: 可迭代[Int] = HashMap(40.30)
// Print all student names and ages
scala> for((x,y) <- map) println(s"$x $y")
lisi 40
zhangsan 30
// Get the age of wangwu, or -1 if wangwu does not exist
scala> map.getOrElse("wangwu".- 1)
res17: Int = - 1
// Add a new student: Wangwu, 35
scala> map + "wangwu"->35
res22: scala.collection.mutable.Map[String.Int] = Map(lisi -> 40, zhangsan -> 30, wangwu -> 35)
// Lisi is removed from the mutable map
scala> map - "lisi"
res23: scala.collection.mutable.Map[String.Int] = Map(zhangsan -> 30)
Copy the code
5. Iterator
Scala provides an iterator for each type of collection to access iteratively
Iterators are used to traverse the collection
- use
iterator
The getIterator () method gets an iterator from the collection - Two basic operations of iterators
- HasNext — Queries whether the next element is present in the container
- Next — Returns the next element of the iterator, and if not, throws NoSuchElementException
- Each iterator is stateful
- Leave it in place of the last element after iteration
- A second use throws a NoSuchElementException
- You can use while or for to return elements one by one
The sample
scala> val ite = a.iterator
ite: Iterator[Int] = non-empty iterator
scala> while(ite.hasNext) {
| println(ite.next)
| }
Copy the code