Created
June 30, 2023 07:42
-
-
Save senamit2708/bef3da8bd38f94125417847281c06c3a to your computer and use it in GitHub Desktop.
Collection Learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package collections | |
| data class Person( | |
| val name: String, | |
| val age: Int, | |
| val driversLicence: Boolean = false | |
| ) | |
| fun main() { | |
| conditionFunctions() | |
| emptyConditionFunctions() | |
| chunckedfunction() | |
| flatMapFun() | |
| zipfun() | |
| } | |
| fun conditionFunctions() { | |
| val friendGroup = listOf( | |
| Person("Amit", 31, true), | |
| Person("Saurav", 12), | |
| Person("Adi", 19, true) | |
| ) | |
| println(friendGroup.any { it.driversLicence }) //true | |
| println(friendGroup.none { it.age>18 }) //false | |
| println(friendGroup.all { it.name.length>2 }) //true | |
| } | |
| fun emptyConditionFunctions(){ | |
| val nobody = emptyList<Person>() | |
| println(nobody.any { it.driversLicence }) //false | |
| println(nobody.none { it.age>18 }) //true | |
| println(nobody.all { it.name.length<2 }) //true //vacous truth | |
| } | |
| fun chunckedfunction() { | |
| val numberlist = listOf(1,2,3,4,5,6,7) | |
| //chuncked function is used to divide the list into parts based on the size given | |
| println(numberlist.chunked(3)) //[[1, 2, 3], [4, 5, 6], [7]] | |
| println(numberlist.chunked(3){ it.reversed()}) //[[3, 2, 1], [6, 5, 4], [7]] | |
| } | |
| fun flatMapFun() { | |
| val nameList = listOf("amit", "sen", "saurav", "singh") | |
| val lettersUsed = nameList.flatMap { | |
| it.toList() | |
| } | |
| println(lettersUsed) //[a, m, i, t, s, e, n, s, a, u, r, a, v, s, i, n, g, h] | |
| } | |
| fun zipfun() { | |
| //zip function is used to zip two list with each other | |
| val namelist = listOf("amit", "saurav", "adi") | |
| val agelist = listOf(31, 17, 24) | |
| val nameAgeMergelist = namelist zip agelist | |
| println(nameAgeMergelist) //[(amit, 31), (saurav, 17), (adi, 24)] | |
| val nameAgeMergeAdvanceList = namelist.zip(agelist) {name, age -> | |
| name.uppercase() to age | |
| } | |
| println(nameAgeMergeAdvanceList) //[(AMIT, 31), (SAURAV, 17), (ADI, 24)] | |
| println(nameAgeMergelist.unzip()) //([amit, saurav, adi], [31, 17, 24]) | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package collections | |
| import kotlin.random.Random | |
| fun main() { | |
| generatingList() | |
| listBasicfunction() | |
| mutableListFun() | |
| } | |
| fun generatingList() { | |
| val listOne = listOf(1, 2, 3, 4, 5, 6) | |
| val listTwo = List(5) { idx -> "No. $idx" } | |
| val listThree = "word_salad".toList() | |
| val listfour = mapOf( | |
| 1 to "Gold", | |
| 2 to "Silver", | |
| 3 to "Diamond" | |
| ).toList() | |
| val listFive = generateSequence { Random.nextInt(100).takeIf { it > 30 } }.toList() | |
| val listSix = (0..10).toList() | |
| println(listOne) //[1, 2, 3, 4, 5, 6] | |
| println(listTwo) //[No. 0, No. 1, No. 2, No. 3, No. 4] | |
| println(listThree)//[w, o, r, d, _, s, a, l, a, d] | |
| println(listfour) //[(1, Gold), (2, Silver), (3, Diamond)] | |
| println(listFive) //[44, 50, 50, 32, 45, 45] | |
| println(listSix) //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
| } | |
| fun listBasicfunction() { | |
| val names = listOf("amit", "sohan", "shyam", "rupesh") | |
| val nameAt5 = names.getOrElse(5) { "no name found at index 5" } | |
| val nameAt7 = names.getOrNull(7) | |
| println("simple get fun indext-2: ${names.get(2)}") //simple get fun indext-2: shyam | |
| println("getOrElse fun index-5: $nameAt5") //getOrElse fun index-5: no name found at index 5 | |
| println("getOrNull fun index-7: $nameAt7") //getOrNull fun index-7: null | |
| } | |
| //iterable -> collection -> list -> mutableList -> hiraercy | |
| fun mutableListFun() { | |
| //creation of mutable list | |
| val mutablenameList = mutableListOf("amit", "mohan", "sohan", "shyam") | |
| val mutablenameList2 = listOf("amit", "mohan", "sohan", "shyam").toMutableList() | |
| val numberList = mutableListOf(1, 2, 3) | |
| numberList.add(4) | |
| numberList += 5 | |
| println(numberList) //[1, 2, 3, 4, 5] | |
| numberList.add(2, 10) | |
| numberList += listOf(6, 7, 8) | |
| println(numberList) //[1, 2, 10, 3, 4, 5, 6, 7, 8] | |
| val numberListTwo = mutableListOf(1, 2, 3, 3, 3, 4, 4, 4, 4, 5) | |
| numberListTwo.remove(3) | |
| numberListTwo -= 5 //element | |
| println(numberListTwo) //[1, 2, 3, 3, 4, 4, 4, 4] | |
| numberListTwo -= listOf(3, 4) //it will remove all the elements 3,4 | |
| println(numberListTwo) //[1, 2] | |
| val numberListThree = mutableListOf(1, 2, 3, 4, 5, 6) | |
| numberListThree.removeAt(3)//use removeAt for index item deletion. | |
| println(numberListThree) //[1, 2, 3, 5, 6] | |
| numberListThree[2] = 4 //changing the element value of index = 2 | |
| println(numberListThree) //[1, 2, 4, 5, 6] | |
| numberListThree.fill(6) //changing every index value to 6. | |
| println(numberListThree) //[6, 6, 6, 6, 6] | |
| numberListThree.clear() // removing every element from the list | |
| println(numberListThree) //[] | |
| val numberListFour = mutableListOf(1, 8, 3, 12, 9, 10) //for mutablelist and normal list | |
| numberListFour.shuffle() | |
| println(numberListFour) //[3, 12, 9, 10, 1, 8] | |
| numberListFour.sort() | |
| println(numberListFour) //[1, 3, 8, 9, 10, 12] | |
| numberListFour.reverse() | |
| println(numberListFour) //[12, 10, 9, 8, 3, 1] | |
| val numberListFive = mutableListOf(1, 3, 7, 4, 9) | |
| numberListFive.removeAll { it > 5 } //it will remove all element greater than 5 | |
| println(numberListFive) //[1, 3, 4] | |
| val numberListSix = mutableListOf(1, 3, 7, 4, 9) | |
| numberListSix.retainAll { it > 5 } //it will keep all element greater than 5 | |
| println(numberListSix) //[7, 9] | |
| val numberListSeven = mutableListOf(1, 3, 5, 2, 7, 9, 4) | |
| val subListSeven = numberListSeven.subList(1, 4) //from index 1 upto index 4 | |
| println(subListSeven) //[3, 5, 2] | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment