Skip to main content

Posts

Showing posts with the label Kotlin List

android kotlin - plusAssign and minusAssign example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnPlusAssign = findViewById<Button>(R.id.btnPlusAssign) val btnMinusAssign = findViewById<Button>(R.id.btnMinusAssign) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() btnList.setOnClickListener { // Create a list with values val list = mutableListOf("RE...

android kotlin - removeAll and retainAll example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnRemoveAll = findViewById<Button>(R.id.btnRemoveAll) val btnRetainAll = findViewById<Button>(R.id.btnRetainAll) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() btnList.setOnClickListener { // Create a list with values val list = mutableListOf("RED...

android kotlin - getOrElse and getOrNull example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnGetOrNull = findViewById<Button>(R.id.btnGetOrNull) val btnGetOrElse = findViewById<Button>(R.id.btnGetOrElse) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = mutableListOf("RED","GREEN","YELLOW",...

android kotlin - Sequence partition example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnPartition = findViewById<Button>(R.id.btnPartition) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = generateList() btnList.setOnClickListener { textView.text = "Employees:\n" // Iterate through the lis...

android kotlin - maxBy and maxWith example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnMaxBy = findViewById<Button>(R.id.btnMaxBy) val btnMaxWith = findViewById<Button>(R.id.btnMaxWith) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = generateList() btnList.setOnClickListener { textView.text = ...

android kotlin - distinctBy multiple fields example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnDistinctBy = findViewById<Button>(R.id.btnDistinctBy) val btnDistinctByFields = findViewById<Button>( R.id.btnDistinctByFields) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = generateList() btnList.setOnClickL...

android kotlin - distinctBy example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnDistinctBy = findViewById<Button>(R.id.btnDistinctBy) val btnDistinctByAge = findViewById<Button>( R.id.btnDistinctByAge) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = generateList() btnList.setOnClickListen...

android kotlin - Distinct example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnDistinct = findViewById<Button>(R.id.btnDistinct) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = listOf("RED","GREEN","RED","BLUE", "PINK","BLUE","BLACK","B...

android kotlin - List groupBy example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnGroupBy = findViewById<Button>(R.id.btnGroupBy) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = listOf("RED","GREEN","YELLOW","MAGENTA", "PINK","WHITE","BLACK",...

android kotlin - joinToString example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import java.util.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnSimple = findViewById<Button>(R.id.btnSimple) val btnJoinToString = findViewById<Button>( R.id.btnJoinToString) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = listOf("RED","GREEN...

android kotlin - forEachIndexed example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnForEachIndexed = findViewById<Button>( R.id.btnForEachIndexed) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with values val list = listOf("RED","GREEN","YELLOW", "MAGENTA","PINK","WHITE...

android kotlin - List of not null example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnNotNullList = findViewById<Button>( R.id.btnNotNullList) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() // Create a list with null values val list = listOf("Red",null,"Green","Yellow", null,"Black","White",nul...

android kotlin - List filter not null example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnFilterNotNull = findViewById<Button>( R.id.btnFilterNotNull) val btnFilterNotNullTo = findViewById<Button>( R.id.btnFilterNotNullTo) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val list = listOf("Red",null,"Green", ...

android kotlin - Sort list with null values example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnNullsFirst = findViewById<Button>( R.id.btnNullsFirst) val btnNullsLast = findViewById<Button>( R.id.btnNullsLast) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() val list = mutableListOf("Red",null,"Green","Yellow", ...

android kotlin - List sort multiple fields by both orders

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnSort = findViewById<Button>(R.id.btnSort) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() btnList.setOnClickListener { val list = generateList() textView.text = "Employees:\n" // Iterate through the list list.forEach { ...

android kotlin - List sort descending using multiple fields

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnSort = findViewById<Button>(R.id.btnSort) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() btnList.setOnClickListener { val list = generateList() textView.text = "Employees:\n" // Iterate through the list list.forEach { ...

android kotlin - Sort list using comparator example

MainActivity.kt package com.cfsuman.kotlintutorials import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the widgets reference from XML layout val textView = findViewById<TextView>(R.id.textView) val btnList = findViewById<Button>(R.id.btnList) val btnSortWith = findViewById<Button>(R.id.btnSortWith) val btnSortWithDesc = findViewById<Button>( R.id.btnSortWithDesc) // make textview content scrollable textView.movementMethod = ScrollingMovementMethod() btnList.setOnClickListener { val list = generateList() textView.text = "Em...

Android Kotlin: How to sort a complex List by multiple fields

This code demonstrates sorting a list of employees by age and then name in an Android application written in Kotlin. It utilizes buttons to trigger displaying the unsorted list and the sorted list with details like name, age, and salary. Breakdown: The MainActivity class handles the activity lifecycle and UI interaction. It defines a generateList function that creates a list of Employee objects with names, ages, and salaries. The Employee data class represents an employee with these attributes. The layout file ( activity_main.xml ) defines the user interface elements - two buttons and a text view. There are two button click listeners: "List" button: Generates an employee list. Iterates through the list and displays employee details in the text view. "sortWith" button: Generates an employee list. Sorts the list by age first and then by name using the sortWith function with compareBy extension. Iterates through the sorted list and displays employee deta...