I have issue when I use GroupBy an ArrayList object in Kotlin.
I have JSON like here:
{
"securityAccountList": [
{
"accountNumber": "001548900001",
"accountName": "Tjandraningrum",
"currency": "IDR",
"debtInstrument": "0",
"equityInstrument": "0"
},
{
"accountNumber": "001548900001",
"accountName": "Tjandraningrum",
"currency": "IDR",
"debtInstrument": "0",
"equityInstrument": "0"
},
{
"accountNumber": "001548900001",
"accountName": "Tjandraningrum",
"currency": "USD",
"debtInstrument": "22",
"equityInstrument": "66"
}
]
}
I generate object by GSON like this:
data class ContentModel(
@SerializedName("securityAccountList") var securityAccountList: ArrayList<SecurityAccountList> = arrayListOf()
)
data class SecurityAccountList(
@SerializedName("accountNumber") var accountNumber: String? = null,
@SerializedName("accountName") var accountName: String? = null,
@SerializedName("currency") var currency: String? = null,
@SerializedName("debtInstrument") var debtInstrument: String? = null,
@SerializedName("equityInstrument") var equityInstrument: String? = null
)
now I want to grouping by field currency and I want result like this
{
"securityAccountList": [
{
"accountNumber": "001548900001",
"accountName": "Tjandraningrum",
"currency": "IDR",
"debtInstrument": "0",
"equityInstrument": "0"
},
{
"accountNumber": "001548900001",
"accountName": "Tjandraningrum",
"currency": "USD",
"debtInstrument": "22",
"equityInstrument": "66"
}
]
}
but the result not changed, still 3 securityAccountList, this my code for grouping:
val newSecurityAccountList= contentModel.securityAccountList
newSecurityAccountList.groupingBy { it.currency }
newSecurityAccountList.sortBy { it.currency }
Note: contentModel.securityAccountList is ArrayList of object JSON the newSecurityAccountList not changed value.
please your advice, sorry for my English.
CodePudding user response:
private fun List<SecurityAccountList>.customFilter(): List<SecurityAccountList> =
groupBy { it.currency }.map {
// HERE you can do whaterver you need.
it.value
.filter { item-> item.currency >= 0 }
.maxByOrNull { item-> item.accountName.length }
}.filterNotNull()
Using in code
val returnlist = yourListBeforeFilter.customFilter()
For more details tell us what exactly you need.
You can as well use here sortBy
CodePudding user response:
It sounds like you are expecting groupingBy
to modify the list. In fact it won't, but will instead return a new Grouping
object which can be used to gather the results into a new, different list.
It can be confusing, because sortBy
does mutate the list, whereas the similar-looking groupBy
and groupingBy
functions return a new object or copy and don't mutate the original.
The best way to be sure whether a function is going to mutate the existing object or create a new object is to check its documentation. In Kotlin, most of the standard library functions are non-mutating, because Kotlin encourages immutability.
You could try replacing your code with something like:
val groupedAccounts = contentModel.securityAccountList.groupBy { it.currency }
Note how the variable stores the result of the call to groupBy
, not the original securityAccountList
.