Posts

Coroutine in kotlin

Image
 

Varargs in kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { // printIntegers(1,2,3,4,5,6) // var a = intArrayOf(1,2,3,4,5,6) // printIntegers(*a) printIntegers ( 1 , 2 , 3 , 4 , 5 , 6 , b= 7 ) var b = arrayOf ( "11" , "21" , "31" , "41" , "51" , "61" ) printStrings (*b) } fun printIntegers ( vararg numbers: Int,b: Int) { println ( "b : $ b " ) for (i in numbers) { println (i) } } fun printStrings ( vararg numbers: String) { for (i in numbers) { println (i) } } OUTPUT : b : 7 1 2 3 4 5 6 11 21 31 41 51 61 Process finished with exit code 0

Generics practical in kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { val obj = GenericsExample <String> ( "Android" ) val obj1 = GenericsExample <Int> ( 10 ) val obj2 = GenericsExample <Double> ( 22.45 ) val obj3 = GenericsExample <Boolean> ( true ) val intBox = Box( 100 ) val stringBox = Box( "Kotlin" ) val doubleBox = Box( 90.56 ) val booleanBox = Box( true ) println ( "Int is : ${ intBox. value } " ) println ( "String is : ${ stringBox. value } " ) println ( "Double is : ${ doubleBox. value } " ) println ( "Boolean is : ${ booleanBox. value } " ) } class GenericsExample< T >(input: T ) { init { println ( "I am getting called with the value : $ input " ) } } class Box< T >( val value : T ) OUTPUT : I am getting called with the value : Android I am getting called with the value : 10 I am getting called with...

Generics in Kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { printValue ( 3 ) printValue ( "Hello World" ) printValue ( 12.42 ) printValue ( true ) var obj = PQR( 13 ) obj.printVal() var obj1 = PQR( "Hello World" ) obj1.printVal() var obj2 = PQR( true ) obj2.printVal() var obj3 = PQR( 12.45 ) obj3.printVal() } fun < T > printValue (value: T ) { println (value) } class PQR< T >( var value : T ) { fun printVal () { println ( "Value is : $ value " ) } } OUTPUT : 3 Hello World 12.42 true Value is : 13 Value is : Hello World Value is : true Value is : 12.45 Process finished with exit code 0

Scope Functions: let , run, with, apply, & also

  package com.example.my_kotlin_practice fun main (args: Array<String>) { // Scope functions // -let // -run // -with // -apply // -also var emp = Employee() // emp.id = 1 // emp.name = "ABC" var x = emp. apply { this . id = 1 this . name = "ABC" } println ( "Apply : ${ emp. id } " ) var emp1:Employee? = Employee() var y = emp1?. let { println ( "Let : ${ it . id } " ) println ( "Let : ${ it . name } " ) // 4 } var z = with (emp) { println ( "With : ${this . name } " ) } var p = emp. run { println ( "Run : ${this . name } " ) } var c = emp. also { println ( "Also : ${ it . name } " ) true } println (c. name ) } class Employee{ var id :Int = 0 var name :String = "" } OUTPUT : Apply : 1 Let : 0 Let : With : ABC Run : ABC Also : ABC ABC ...

Collection Functions in Kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { var list = listOf ( "MSC" , "Madam" , "civic" , "CS" , "radar" ) // var palindromeStrings = list.filter(::isPalindrome) var palindromeStrings = list. filter { isPalindrome ( it ) } println (palindromeStrings) var list1 = list. map { it . uppercase () } println (list1) list. forEach { println ( it ) } println ( "Total number of elements : ${ list. count () } " ) } fun isPalindrome (str: String): Boolean { return str. lowercase () == str. lowercase (). reversed () } OUTPUT : [Madam, civic, radar] [MSC, MADAM, CIVIC, CS, RADAR] MSC Madam civic CS radar Total number of elements : 5 Process finished with exit code 0

Lambdas Expressions in Kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>) { // Kotlin lambda is a function which has no name and defined with a curly braces {} which takes zero or more parameters and // body of function. // The body of function is written after variable (if any) followed by -> operator. // Syntax : {variable with type -> body of the function} val upperCase = { str:String -> str. uppercase () } println (upperCase( "hello world!" )) var lambdaFunction1 = { a:Int,b:Int -> a+b } println (lambdaFunction1( 2 , 3 )) var lambdaFunction2 = { msg:String -> println ( "Hello World" ) println ( "Message is $ msg " ) } lambdaFunction2( "Hello" ) var lambdaMinus:(Int,Int)-> Int = { x,y -> x-y } var unitLambda = { msg: String -> println (msg) } var squareLambda:(Int)->Int = { it -> it*it } println ( "Square is : ${ squareLambda( 9 ) } ...

Higher Order Functions and Function Types in Kotlin With Examples

package com.example.my_kotlin_practice fun main (args: Array<String>) { // A higher-order function is a function that takes another function as parameter and/or returns a function. println ( square ( 2 )) var sq:(Int)->Int = ::square println ( "Square of 8 is : ${ sq( 8 ) } " ) var result = calculate4 ( 4 , 5 ,::sum1) println ( "Result is : $ result " ) var a = 10 var b = 20 arithmeticOperation (a,b,::add) arithmeticOperation (a,b,::sub) arithmeticOperation (a,b,::mul) arithmeticOperation (a,b,::div) } fun square (num: Int): Int { return num*num } fun sum1 (a:Int,b: Int) = a + b fun calculate4 (a:Int,b:Int, operation:(Int,Int) -> Int):Int { return operation(a,b) } fun add (x:Int,y: Int): Int { return x + y } fun sub (x:Int,y: Int): Int { return x - y } fun mul (x:Int,y: Int): Int { return x * y } fun div (x:Int,y: Int): Int { return x/y } fun arithmeticOperation (a:Int,b:Int,fn:(I...

Collections (List, Set & Map) in Kotlin

  package com.example.my_kotlin_practice fun main (args: Array<String>){ var list = listOf ( 1 , 2 , 3 , 4 , 5 ) println (list) println (list.contains( 6 )) println (list.contains( 5 )) println (list.get( 0 )) println (list[ 0 ]) var list1 = mutableListOf ( 1 , 2 , 3 , 4 , 5 ) list1[ 1 ] = 11 println ( "'11' is added at '1st' position : $ list1 " ) list1.set( 2 , 10 ) println ( "'10' is added at 2nd position in 'list1' : $ list1 " ) list1.addAll(list) println (list1) val subjectSet = setOf ( "CS" , "Physics" , "Chemistry" , "Maths" , "CS" , "Physics" ) println (subjectSet) println (subjectSet. elementAt ( 3 )) println (subjectSet.contains( "English" )) val subjectSet1 = mutableSetOf ( "CS" , "Physics" , "Chemistry" , "Maths" , "CS" , "...