
This section was inspired by GS Collections Kata.
Kotlin can be easily mixed with Java code. Default collections in Kotlin are all Java collections under the hood. Learn about read-only and mutable views on Java collections.
The Kotlin standard library contains lots of extension functions that make working with collections more convenient. For example, operations that transform a collection into another one, starting with 'to': toSet or toList.
Implement the extension function Shop.getSetOfCustomers(). The class Shop and all related classes can be found in Shop.kt.
fun Shop.getSetOfCustomers(): Set<Customer> =
TODO()
data class Shop(val name: String, val customers: List<Customer>)
data class Customer(val name: String, val city: City, val orders: List<Order>) {
override fun toString() = "$name from ${city.name}"
}
data class Order(val products: List<Product>, val isDelivered: Boolean)
data class Product(val name: String, val price: Double) {
override fun toString() = "'$name' for $price"
}
data class City(val name: String) {
override fun toString() = name
}
fun Shop.getSetOfCustomers(): Set<Customer> = this.customers.toSet()
Shop 클래스에 확장 함수 getSetOfCustomers를 구현하여, Shop 객체에 포함된 고객(Customer)들을 Set으로 반환하는 문제이다.
Shop : 가게 이름과 고객 리스트를 포함하는 클래스Customer : 고객명, 도시, 주문 목록을 포함하는 클래스Order : 상품 목록과 배달 여부를 포함하는 클래스Product : 상품 이름, 가격을 포함하는 클래스City : 도시명을 포함하는 클래스Shop 클래스는 고객 리스트를 List 형태로 가지고 있다. 문제에서 Set 으로 고객 리스트를 반환하는 것이 목적이므로
List 를 Set 으로 toSet() 을 이용하여 변경해준다.
컬렉션을 다른 타입의 컬렉션이나 배열로 변환하는 데 사용되는 함수들
toBooleanArraytoByteArraytoCharArraytoCollectiontoDoubleArraytoFloatArraytoHashSettoIntArraytoListtoLongArraytoMaptoMutableListtoMutableMaptoMutableSettoPairtoPropertiestoSettoShortArraytoSortedMaptoSortedSettoStringtoTypedArraytoUByteArraytoUIntArraytoULongArraytoUShortArray