object Demo{
def main(args:Array[String]):Unit = {
println("Welcome to Scala World!")
}
}
Compile the scala code
D:\iEd>scalac scala1.scala (File name is scala1.scala but class name is Demo)
Run it
D:\iEd>scala Demo // specify class name but not file name
Welcome to Scala World!
object Demo3{
def main(arg:Array[String]):Unit = {
val x:Int = 20
val y:Int = 30
println(x+y)
}
}
object UserInput {
def main(args:Array[String]):Unit = {
println("Enter first number")
val a:Int = scala.io.StdIn.readInt()
println("Enter second number")
val b:Int = scala.io.StdIn.readInt()
println("The multiplication is "+(a*b))
}
}
D:\iEd>scalac UserInput.scala
D:\iEd>scala UserInput
Enter first number
3
Enter second number
5
The multiplication is 15
object collectionDemo{
def main(args:Array[String]):Unit = {
val a1:Array[Int] = Array(1,2,3,4,5)
println(a1)
val a2:Array[Float] = Array(2.3f,4.5f,5.6f)
println(a2)
val a3:Array[String] = Array("abc","xyz","pqr")
println(a3)
val l1:List[Int] = List(34,23,45,43,22)
println(l1)
val l2:List[Double]=List(2.3,3.4,5.6)
println(l2)
val l3:List[String]=List("USA","India","China")
println(l3)
val s1:Seq[Int] = Seq(45,56,23)
println(s1)
val s2:Seq[Boolean] = Seq(true,false,true)
println(s2)
val m1:Map[Int,String] = Map((1,"USA"),(2,"China"),(3,"India"))
println(m1(1))
println(m1(2))
val r1:Range = Range(1,100)
println(r1)
}
}
D:\iEd>scalac collectionDemo.scala
D:\iEd>scala collectionDemo
[I@dbf57b3
[F@384ad17b
[Ljava.lang.String;@61862a7f
List(34, 23, 45, 43, 22)
List(2.3, 3.4, 5.6)
List(USA, India, China)
List(45, 56, 23)
List(true, false, true)
USA
China
Range 1 until 100
a1: Array[Int] = Array(2, 3, 4, 5, 6, 7, 8)
scala> a1.size
res0: Int = 7
scala> a1(0)
res1: Int = 2
scala> a1(5)
res2: Int = 7
scala> a1.update
update updated
scala> a1.updated(6,60)
res3: Array[Int] = Array(2, 3, 4, 5, 6, 7, 60)
scala> a1.updated(5,50)
res4: Array[Int] = Array(2, 3, 4, 5, 6, 50, 8)
scala> a1(0) = 100
scala> a1
res7: Array[Int] = Array(100, 3, 4, 5, 6, 7, 8)
scala> val myList = List(23,34,45,56,67,78)
myList: List[Int] = List(23, 34, 45, 56, 67, 78)
scala> myList.size
res8: Int = 6
scala> myList(4)
res9: Int = 67
scala> myList.updated(0,200)
res11: List[Int] = List(200, 34, 45, 56, 67, 78)
scala> val myList:List[Int] = List(1,2,3,4,5)
myList: List[Int] = List(1, 2, 3, 4, 5)
scala> val m1 = Map( ("name","scala"), ("version","2.11"),("type","oops+fp"))
m1: scala.collection.immutable.Map[String,String] = Map(name -> scala, version -
> 2.11, type -> oops+fp)
scala> m1.size
res14: Int = 3
scala> m1("name")
res16: String = scala
scala> m1("version")
res17: String = 2.11
scala> m1("type")
res18: String = oops+fp
Map contains Key,value pairs
"name","version","type" --> Keys
"scala","2.11","oops+fp" --> values
scala> val m1:Map[String,String] = Map( ("Rajini"->"Kanth"),("Kamal"->"Hassan"))
m1: Map[String,String] = Map(Rajini -> Kanth, Kamal -> Hassan)
scala> m1("Rajini")
res19: String = Kanth
scala> m1("Kamal")
res20: String = Hassan
scala> val m2:Map[Int,String] = Map( (1,"Hyd"),(2,"Del"),(3,"Bng"))
m2: Map[Int,String] = Map(1 -> Hyd, 2 -> Del, 3 -> Bng)
scala> val m1:Map[String,String] = Map ( ("name"->"sara"),("city","bangalore"))
m1: Map[String,String] = Map(name -> sara, city -> bangalore)
scala> m1("name") + " " + m1("city")
res21: String = sara bangalore
Tuple : to combine multiple elements
Tuple is not a collection.
It is simply combination of elements
scala> val t1 = (34,45,56,78)
t1: (Int, Int, Int, Int) = (34,45,56,78)
scala> val t1:(Int,Int,Int,Int) = Tuple4(34,45,56,78)
t1: (Int, Int, Int, Int) = (34,45,56,78)
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)
scala> val b = List(4,5,6)
b: List[Int] = List(4, 5, 6)
scala> val c = Map( ("city","Delhi"), ("position","HR"))
c: scala.collection.immutable.Map[String,String] = Map(city -> Delhi, position -
> HR)
scala> val t = (a,b,c)
t: (Array[Int], List[Int], scala.collection.immutable.Map[String,String]) = (Arr
ay(1, 2, 3),List(4, 5, 6),Map(city -> Delhi, position -> HR))
scala> t
res25: (Array[Int], List[Int], scala.collection.immutable.Map[String,String]) =
(Array(1, 2, 3),List(4, 5, 6),Map(city -> Delhi, position -> HR))
scala> t._1
res26: Array[Int] = Array(1, 2, 3)
scala> t._1
res28: Array[Int] = Array(1, 2, 3)
scala> t._1(1)
res29: Int = 2
scala> t._2
res30: List[Int] = List(4, 5, 6)
scala> t._2(2)
res31: Int = 6
scala> t._3
res32: scala.collection.immutable.Map[String,String] = Map(city -> Delhi, positi
on -> HR)
scala> t._3("position")
res33: String = HR
scala> val a1 = Array(1,2,3,4)
a1: Array[Int] = Array(1, 2, 3, 4)
scala> val a1:Array[Any] = Array(1,"2",3.0)
a1: Array[Any] = Array(1, 2, 3.0)
scala> val l:List[Any] = List("3","Agra",3.0)
l: List[Any] = List(3, Agra, 3.0)
Any -> Homogeneous, Hetrogeneous
Hybrid
scala> val t2 = (23,"Spark",true)
t2: (Int, String, Boolean) = (23,Spark,true)
scala> val t = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
<console>:1: error: too many elements for tuple: 23, allowed: 22
val t = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23)
^
// Maximum elements in a tuple is 22 only
scala> val t = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
t: (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, I
nt, Int, Int, Int, Int, Int, Int) = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,1
8,19,20,21,22)
scala> val s1:Seq[Int] = Seq(23,45,67,7)
s1: Seq[Int] = List(23, 45, 67, 7)
scala> val s1:Seq[Double] = Seq(23,45,67,7.8f)
s1: Seq[Double] = List(23.0, 45.0, 67.0, 7.800000190734863)
scala> val s1:Seq[Float] = Seq(23,45,67,7.8f)
s1: Seq[Float] = List(23.0, 45.0, 67.0, 7.8)
object conditional{
def main(args:Array[String]):Unit ={
println("Enter a number : ")
var x = scala.io.StdIn.readInt()
if (x % 2 == 0)
println("Even Number")
else
println("Odd Number")
}
}
D:\iEd>scalac conditional.scala
D:\iEd>scala conditional
Enter a number :
3
Odd Number
object GradeCalc{
def main(args:Array[String]): Unit = {
println("Enter the name of the student : ")
val name:String = scala.io.StdIn.readLine()
println("Enter the percentage of marks")
val p:Float = scala.io.StdIn.readFloat()
if (p > 70)
println("A+ Grade")
else if ( p >= 60 && p <70)
println("A Grade")
else if ( p >= 50 && p <60)
println("B Grade")
else
println("C Grade")
}
}
D:\iEd>scala GradeCalc
Enter the name of the student :
55
Enter the percentage of marks
55
B Grade
object GradeCalc{
def main(args:Array[String]): Unit = {
println("Enter the name of the student : ")
val name:String = scala.io.StdIn.readLine()
println("Enter the percentage of marks")
val p:Float = scala.io.StdIn.readFloat()
if (p > 100 || p <= 0){
println("Wrong value.. please reenter")
System.exit(1)
}
if (p > 70)
println("A+ Grade")
else if ( p >= 60 && p <70)
println("A Grade")
else if ( p >= 50 && p <60)
println("B Grade")
else
println("C Grade")
}
}
D:\iEd>scala GradeCalc
Enter the name of the student :
sare
Enter the percentage of marks
10000
Wrong value.. please reenter
object dynaArray{
def main(args:Array[String]):Unit={
println("Enter Size of an Array : ")
val s:Int = scala.io.StdIn.readInt()
val a:Array[Int] = new Array[Int](s)
println("Enter the elements of the array")
for (i <- 0 to a.size-1)
a(i) = scala.io.StdIn.readInt()
println("The array is " + a.mkString(","))
}
}
D:\iEd>scala dynaArray
Enter Size of an Array :
3
Enter the elements of the array
3
4
6
The array is 3,4,6
D:\iEd>scala dynaArray
Enter Size of an Array :
7
Enter the elements of the array
1
9
2
8
3
7
5
The array is 1,9,2,8,3,7,5
Wednesday, 9 January 2019
Subscribe to:
Comments (Atom)
Flume - Simple Demo
// create a folder in hdfs : $ hdfs dfs -mkdir /user/flumeExa // Create a shell script which generates : Hadoop in real world <n>...
-
How to fetch Spark Application Id programmaticall while running the Spark Job? scala> spark.sparkContext.applicationId res124: String = l...
-
input data: ---------- customerID, itemID, amount 44,8602,37.19 35,5368,65.89 2,3391,40.64 47,6694,14.98 29,680,13.08 91,8900,24.59 ...
-
pattern matching is similar to switch statements in C#, Java no fall-through - at least one condition matched no breaks object PatternExa { ...