[英]Filtering the list of objects using the Map in Kotlin
I am trying to create a dictionary filter that will filter the incoming list of parameters.我正在尝试创建一个字典过滤器来过滤传入的参数列表。
The problem is that when there are parameters in the list that should be filtered and removed - I get a NullPointerException.问题是,当列表中有应该过滤和删除的参数时 - 我得到一个 NullPointerException。
Basic informations:基本信息:
This is how filter looks:这是过滤器的外观:
data class Filter(
val filters: Map<ParameterName, Set<String>>
)
data class ParameterName(
val key: String
)
Example of filter:过滤器示例:
Filter(filters={ParameterName(key=Manufacturer)=[brand, producer]})
The filtered list ( List<Parameter>
) has the following structure:过滤后的列表 (
List<Parameter>
) 具有以下结构:
data class Parameter(
val id: String?,
val name: String?,
val values: List<String>?
)
Example of List<Parameter>
List<Parameter>
示例
[Parameter(id=123, name=brand, values=[Nike]), Parameter(id=345, name=color, values=[black, pink]), Parameter(id=823, name=test, values=[some, thing])]
The logic is that the filtered list is changed to Map <String, ParameterValue>.逻辑是将过滤后的列表更改为 Map <String, ParameterValue>。 Where:
在哪里:
a) String (key) is the value of the filter key (The key in the filter map has the main value and any other names under it. For example - under Manufacturer we have got values "brand" or "producer") a)字符串(键)是过滤器键的值(过滤器映射中的键有主值和它下面的任何其他名称。例如 - 在制造商下我们有值“品牌”或“生产者”)
b) The parameter value (eg Nike) is assigned to the ParameterValue object (val value: String). b) 将参数值(例如 Nike)分配给 ParameterValue 对象(val 值:String)。
What have I prepared:我准备了什么:
fun filterParameters(
parametersList: List<Parameter>,
filters: Map<ParameterName, Set<String>>
): Map<String, ParameterValue> {
return parametersList.associate { param ->
filters.filter {
it.value.first().equals(param.name, ignoreCase = true)
}.keys.first().key to ParameterValue(value = param.values!!.first())
}.toMap()
}
Flow example:流程示例:
listOf(
Parameter(
"123",
"brand",
listOf("Nike")
),
Parameter(
"345",
"color",
listOf("black", "pink")
),
Parameter(
"823",
"test",
listOf("some", "thing")
)
)
b) Filter: b) 过滤器:
mapOf(
ParameterName("Manufacturer") to setOf("brand", "producer")
)
2)Expected output: 2)预期输出:
{Manufacturer=ParameterValue(value=Nike)}
Expected output structure:预期输出结构:
Map<String, ParameterValue>
where在哪里
data class ParameterValue(
val parameterId: String? = null,
val value: String,
val valueId: String? = null
) )
So we just fill value - String所以我们只填充值 - 字符串
data class Parameter(
val id: String?,
val name: String?,
val values: List<String>?
)
val parameters = listOf(
Parameter("123", "brand", listOf("Nike")),
Parameter("3433", "color", listOf("red", "black")),
Parameter("22313", "model", listOf("air max")),
Parameter("2312", "origin", listOf("USA")),
Parameter(null, "size", listOf("7", "7.5", "8", "9")),
Parameter("987", null, listOf("lalala")),
Parameter("999", "code", null),
Parameter(null, null, null)
)
val filters = mapOf(
"Manufacturer" to setOf("brand", "producer"),
"Country" to setOf("origin", "from")
)
fun getValuesForParameterName(parameters: List<Parameter>, filters: Map<String, Set<String>>): Map<String, List<String>> {
return parameters
.filter { parameter -> parameter.name in filters.flatMap { map -> map.value } }
.map { parameter -> filters.filter { map -> map.value.contains(parameter.name) }.keys.first() to parameter.values }
.groupBy { (name, _) -> name }
.map { (name, values) -> name to values.map { (_, list) -> list ?: emptyList() }.flatten() }
.toMap()
}
val result = getValuesForParameterName(parameters, filters)
println(result) // Output: {Manufacturer=[Nike], Property=[USA]}
println(result["Manufacturer"]) // Output: [Nike]
println(result["Country"]) // Output: [USA]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.