The use of Scala tuple tuple

Description

  • A tuple can have up to 22 elements.

Create tuple

val tuple1: (String, Int, String, String) = ("zhangsan", 21, "man", "beijing")

Access the elements in the tuple

println(tuple1._1)
println(tuple1._2)
println(tuple1._3)
println(tuple1._4)

Access via index

println(tuple1.productElement(1))

Iterate over tuples

for (elem <- tuple1.productIterator) {
    
    
  println(elem)
}

Dual tuple

  • There are only two elements in the tuple.
val tuple2: (String, String) = ("1001","zhangsan")
println(tuple2)

Guess you like

Origin blog.csdn.net/FlatTiger/article/details/114583653