This file contains 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
val blank = " ".isBlank() // true | |
val first = "Kaushal.Dhruw".substringBefore('.') // "Kaushal" | |
val last = "Kaushal.Dhruw".substringAfter('.') // "Dhruw" | |
val addSpaces = "1".padStart(2) // " 1" | |
val addSpacesEnd = "1".padEnd(3, '0') // "100" | |
val dropStart = "Dhruw".drop(2) // "ruw" | |
val dropEnd = "Dhruw".dropLast(2) // "Dhr" |
This file contains 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
class NEFT: Transaction ... // class NEFT extends Transaction | |
class IMPS: Transaction ... // class IMPS extends Transaction | |
class RTGS: Transaction ... // class RTGS extends Transaction | |
val transaction: Transaction = getCurrentTransaction() | |
// Now we don't know the type of transaction. It could be either of NEFT, IMPS or RTGS | |
// So we need to identify the type of trsaction and process it accordingly | |
when (transaction) { | |
is NEFT -> transaction.processNEFT() |
This file contains 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
// Java POJO | |
public class Person { | |
private String name; | |
private int age; | |
private char gender; | |
public Person(String name, int age, char gender) { | |
this.name = name; | |
this.age = age; | |
this.gender = gender; |
This file contains 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
fun main (args: Array<String>) { | |
val x = arrayListOf("A.B", "B.C", "C.D", "D.E", "E.F") | |
println(x.map { stringStirrer(it) }) | |
// above line prints [Axx, Bxx, Cxx, Dxx, Exx] | |
} | |
fun stringStirrer(param: String): String { | |
var changes = param.substringBefore(".") | |
changes += "xx" | |
return changes |
This file contains 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
import android.content.Context | |
import android.content.SharedPreferences | |
private fun doSomething(context: Context) { | |
val prefs = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE).edit() | |
prefs.persist(key = "my_name", value = "Kaushal Dhruw") | |
} | |
fun SharedPreferences.Editor.persist(key: String, value: String) { | |
putString(key, value) |
This file contains 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
import android.database.sqlite.SQLiteDatabase | |
// Add an extension function to perform SQLite operations | |
// This is a higher order function as it takes a function as parameter (operation) | |
// @param operation: a function that takes no parameter and returns nothing | |
fun SQLiteDatabase.performDBTransaction(operation: () -> Unit) { | |
beginTransaction() | |
operation() // this is the passed function | |
endTransaction() | |
} |
This file contains 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
// Add a function toINR() to kotlin's Double type | |
fun Double.toINR() = this * oneDollarInINR | |
// Yes it is a single line function | |
val netAmountDeposited = 230.0 | |
println("Net amount deposited(in INR): ${netAmountDeposited.toINR()}") | |
// Did you notice the string interpolation above | |
This file contains 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
// THE KOTLIN WAY | |
// 1. Who did maximum number of transactions | |
val maxTransactionsBy = customers.maxBy { it.transactions.count() } | |
// List all transactions irrespective of customers | |
val transactions = customers.flatMap { it.transactions } | |
// 2a. Get total amount deposited in the bank (use transactions constant from above) | |
val totalAmountDeposited = transactions.filter { it.type == "deposit" }.sumByDouble { it.amount } |
This file contains 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
// JAVA WAY - Who has minimum balance | |
Customer minBalanceCustomer = Collections.min( | |
customers, (o1, o2) -> (int) (o1.getBalance() - o2.getBalance()) | |
); |
This file contains 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
Bank bank = .... ; | |
List<Customer> customers = bank.getCustomers(); // Get all bank customers | |
List<Transaction> = customer.getTransactions(); // Get all transactions by 1 customer | |
double amount = transaction.getAmount(); // Get transaction amount | |
String transactionType = transaction.getType(); // Get type of transaction (deposit or withdraw) |
NewerOlder