Rok Koncina
05/15/2019, 9:58 AMval list = mutableListOf<Item>()
.apply {
add(getItem())
addAll(getMultipleItems())
add(getItem())
}
fun getItem() = Item()
fun getMultipleItems() = listOf(Item(), Item(), Item())
☝️ this is the closest I've gotgildor
05/15/2019, 10:01 AMval list: List<Item> = mutableListOf<Item()…
gildor
05/15/2019, 10:01 AMwbertan
05/15/2019, 10:02 AM@Test
fun asasTest1() {
val list = List(10) { it }
val result: MutableList<Int> = list + 99 //Compiling error
}
@Test
fun asasTest2() {
val mutableList: MutableList<Int> = mutableListOf<Int>(1, 2, 3)
val result: List<Int> = mutableList + 99 //Compiles and it works
}
Rok Koncina
05/15/2019, 10:03 AMRok Koncina
05/15/2019, 10:04 AMval list = listOf(
1,
2,
listOf(3, 4, 5),
6
)
wbertan
05/15/2019, 10:06 AMspread
operator?
@Test
fun asasTest() {
val list = List(10) { it }
val item1 = 90
val item2 = 91
val result: List<Int> = listOf(item1, item2, *(list.toTypedArray()))
}
diesieben07
05/15/2019, 10:07 AMgildor
05/15/2019, 10:08 AMaka how do I do thiswhat is your real code, it’s not clear from this example. Version from your original sample is good IMO, not sure what exactly you want to improve The only efficient way is to create mutable list and add elements there, all other required creation of intermediate lists and copy of data
wbertan
05/15/2019, 10:11 AM@Test
fun asasTest() {
val list = List(10) { it }
val item1 = 90
val item2 = 91
val result: List<Int> = list + listOf(item1, item2)
}
Rok Koncina
05/15/2019, 10:17 AMgildor
05/15/2019, 10:19 AMRok Koncina
05/15/2019, 10:20 AMval items: List<Item> = mutableListOf<Item?>()
.apply {
add(getHeader(it))
add(getChangeDate(it))
addAll(getProducts(it))
add(getTotal(it))
add(getCancelButton(it))
}.filterNotNull()
gildor
05/15/2019, 10:21 AMdiesieben07
05/15/2019, 10:22 AMgetProducts(it).filterNotNullTo(this)
to directly filter the products and use null checks on the other values. But that's usually not worth it.gildor
05/15/2019, 10:22 AMmutableListOf<Item?>()
-> mutableListOf<Item>()
gildor
05/15/2019, 10:22 AMaddIfNonNull
Rok Koncina
05/15/2019, 10:23 AMgildor
05/15/2019, 10:23 AMgetProducts(it).filterNotNullTo(this)
use:
getProducts(it).forEach {
if (it != null) add(it) // or use `addIfNonNull`
}
gildor
05/15/2019, 10:24 AMdiesieben07
05/15/2019, 10:24 AMdiesieben07
05/15/2019, 10:24 AMdiesieben07
05/15/2019, 10:24 AMgildor
05/15/2019, 10:24 AMgildor
05/15/2019, 10:24 AMRok Koncina
05/15/2019, 10:25 AMpublic fun <T : Any> Iterable<T?>.filterNotNull(): List<T> {
return filterNotNullTo(ArrayList<T>())
}
gildor
05/15/2019, 10:25 AMgildor
05/15/2019, 10:25 AMfilterNotNullTo
directlyRok Koncina
05/15/2019, 10:26 AMgildor
05/15/2019, 10:26 AMval items: List<Item> = mutableListOf<Item>()
.apply {
addIfNonNull(getHeader(it))
addIfNonNull(getChangeDate(it))
getProducts(it).filterNotNullTo(this)
addIfNonNull(getTotal(it))
addIfNonNull(getCancelButton(it))
}
gildor
05/15/2019, 10:26 AMdiesieben07
05/15/2019, 10:26 AMfilterNotNull
at the end is not need 😉gildor
05/15/2019, 10:26 AMgildor
05/15/2019, 10:29 AMArrayList<Item>(expectedCapacity)
instead of mutableListOf<Item>()
Paulius Ruminas
05/15/2019, 11:10 AMRok Koncina
05/15/2019, 11:37 AMRok Koncina
05/15/2019, 11:37 AMval items: List<OrderModificationItem> = mutableListOf<OrderModificationItem>()
.addNonNull(getOrderHeader(it))
.addNonNull(getChangeDeliveryDate(it))
.addAllNonNull(getProducts(it))
.addNonNull(getOrderTotal(it))
.addNonNull(getCancelOrderButton(it))
Rok Koncina
05/15/2019, 11:37 AMprivate fun <E> MutableList<E>.addNonNull(e: E?): MutableList<E> {
if (e != null) add(e)
return this
}
private fun <E> MutableList<E>.addAllNonNull(list: List<E?>): MutableList<E> {
list.forEach { addNonNull(it) }
return this
}
gildor
05/15/2019, 11:40 AMRok Koncina
05/15/2019, 12:42 PMgildor
05/15/2019, 12:50 PM