Skip to content

Instantly share code, notes, and snippets.

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"
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()
// 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;
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
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)
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()
}
// 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
// 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 }
@drulabs
drulabs / MinBalanceCustomer.java
Last active August 7, 2018 13:03
Find customer with minimum balance
// JAVA WAY - Who has minimum balance
Customer minBalanceCustomer = Collections.min(
customers, (o1, o2) -> (int) (o1.getBalance() - o2.getBalance())
);
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)