Scala - How to merge two Seq[String], Seq[Double] to Seq[(String,Double)] Code Example

The code for How to merge two Seq[String], Seq[Double] to Seq[(String,Double)]

object MyClass {
    def main(args: Array[String]) {
        val s1 = List("Hello", "Hi", "Hey")
        println("s1:")
        println(s1)
        
        val s2 = List(100, 200, 300)
        println("s2:")
        println(s2)
        
        val result1 = s1.zip(s2)
        println("result1:")
        println(result1)
        
        val result2 = s1.zip(s2).toMap
        println("result2:")
        println(result2)
    }
}

/*
Output:
s1:
List(Hello, Hi, Hey)
s2:
List(100, 200, 300)
result1:
List((Hello,100), (Hi,200), (Hey,300))
result2:
Map(Hello -> 100, Hi -> 200, Hey -> 300)
*/
Code by IncludeHelp, on August 14, 2022 20:26

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.