Scala Tuple (Return Multiple Values)

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-10 09:46:38 Viewed : 178


Scala Tuple (Return Multiple Values) 

Use tuples to store multiple variables together. Return a tuple from a function.

Tuples let you put a heterogenous collection of elements in a little container. Tuples can contain between two and 22 values, and they can all be different types. For example, given a Person class like this: 

class Person(var name: String) 

You can create a tuple that contains three different types like this:

val t = (11, "Eleven", new Person("Eleven"))

You can access the tuple values by number:

t._1

t._2

t._3 

Or assign the tuple fields to variables:

val (num, string, person) = (11, "Eleven", new Person("Eleven"))  

scala.Tuple2:

A tuple of 2 elements; the canonical representation of a scala.Product2.

Parameters

_1

Element 1 of this Tuple2 

_2

Element 2 of this Tuple2

Example:

Following example illustrates about Scala tuple

Save the file as −  TupleExApp.scala  

TupleExApp.scala 

 package runnerdev

/**Scala program that uses tuples*/

object TupleExApp extends App {

  // Create and print two tuples.

  val identity1 = ("Cat"10)

  val identity2 = ("Dog"20) 

  println(identity1)

  println(identity2) 

  // Get first and second items from a tuple.

  val first = identity1._1

  val second = identity1._2 

  println(first)

  println(second)

} 

 

compile and run the above example as follows 

scala> scalac TupleExApp.scala

scala> scala TupleExApp 

Output:

(Cat,10)

(Dog,20)

Cat

10 

Example 2:

Following example illustrates about Scala tuple program that returns from function

Save the file as −  TupleExApp2.scala 

TupleExApp2.scala 

 package runnerdev

/**Scala program that uses tuples*/

object TupleExApp2 extends App { 

  // This def returns a two-item tuple.

  // It uppercases the argument String.

  def upperName(nameString): (StringString) = (name.toUpperCase(), name) 

  // Call upperName function with String.

  val name = "scala Porgram"

  val upperRes = upperName(name)

  println(upperRes) 

  //Scala program that unpacks tuple

  // Return a tuple.

  def multiplyNum(x: Int): (Int, Int) = (x * 3x * 4) 

  // Unpack the tuple returned by the function.

  val (number1number2) = multiplyNum(5)

  println(number1)

  println(number2) 

 

compile and run the above example as follows 

scala> scalac TupleExApp2.scala

scala> scala TupleExApp2 

Output:

(SCALA PORGRAM,scala Porgram)

15

20 

Example 3:

Following example illustrates about Scala program that uses tuple with match  case

Save the file as −  TupleExApp3.scala 

TupleExApp3.scala 

 package runnerdev

/** Scala program that uses tuple with match  */

object TupleExApp3 extends App { 

  def matchMethod(animal: (String, Int)) = {

    animal match {

      case ("dog"size) =>

        println("dog weighs"size)

      case (animalsize) =>

        println("Other animal"animalsize)

    }

  } 

  // Call test method with tuple argument.

  val animal1 = ("dog"60)

  matchMethod(animal1) 

  val animal2 = ("cat"30)

  matchMethod(animal2)

} 

compile and run the above example as follows 

scala> scalac ForLoopAndYield.scala

scala> scala ForLoopAndYield 

Output:

(dog weighs,60)

(Other animal,cat,30) 

Example 3:

Following example illustrates about Scala program that uses list of tuples

Save the file as −  TupleExApp5.scala

  TupleExApp5.scala 

 package runnerdev

/** Scala program that uses list of tuples  */

object TupleExApp5 extends App { 

  // Create list of tuples.

  val employee = List(("Bonam"101"Bangalore"), ("Prasad"102"Singapore"),("Ram"103"Singapore")) 

  // Loop over tuples.

  for (value <- employee) {

    println(value._1 + " from " + value._3)

    println(" employeeId " + value._2)

  }

} 

compile and run the above example as follows 

scala> scalac TupleExApp5.scala

scala> scala TupleExApp5 

Output :

Bonam from Bangalore

  employeeId 101

Prasad from Singapore

  employeeId 102

Ram from Singapore

                employeeId 103   

Search
Sub-Categories
Related Articles

Leave a Comment: