Skip to content

Instantly share code, notes, and snippets.

@PatilShreyas

PatilShreyas/1Models.kt Secret

Last active Dec 30, 2020
Embed
What would you like to do?
data class Customer(val name: String, val city: String, val orders: List<Order>)
data class Order(val products: List<Product>, val isDelivered: Boolean)
data class Product(val product: String, val price: Double)
fun getAllCustomers(): List<Customer> {...}
fun main() {
val customers = getAllCustomers()
// Shuffle customers
val count = customers.shuffled()
// Get any random customer
val random = customers.random()
// Find customer by name
val johnDoe = customers.find { it.name == "John Doe" }
// Get all cities of customers
val cities = customers.map { it.city }.toSet()
// Group customers by City
val customerByCities = customers.groupBy { it.city } // Returns Map<String, List<Customer>>
// Get delivered orders of John Doe
val deliveredOrders = johnDoe.orders.filter { it.isDelivered }
// Get products ordered by John Doe
val productsOrderedByJohnDoe = johnDoe.orders
.flatMap { it.products }
.map { it.product }
// Get sum total of products ordered by John Doe
val totalExpense = johnDoe.orders
.flatMap { it.products }
.sumByDouble { it.price }
// Get the most expensive product ordered by John Doe
val mostExpensive = johnDoe.orders
.flatMap { it.products }
.maxByOrNull { it.price }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment