Kotlin 使用其他列表过滤嵌套列表
Kotlin filtering nested lists with other list
我有一个这样的列表:
val list = List<FlightRecommendationQuery>
在那:
data class FlightRecommendationQuery(
val segments: List<Segment>
)
data class Segment(
val stops: Int?
)
我有另一个列表,其大小与 FlightRecommendationQuery 中的段大小相同,称为过滤器:
val filter = List<Filter>
data class Filter(
val stops: Int?
)
我想过滤 'list' 当过滤器的停止点等于列表中的每个段时。这里,FlightRecommendationQuery 的列表 'filter' 的大小和 'segments' 的大小相同。
更新:我总结了这个解决方案:
list.filter{query->
filter.indices.all{
if(filter[it].stops==null) true
else
filter[it].stops==query.segments[it].stops
}
}
我不确定是否有更有效的方法,但我认为可以这样做:
val filteredList = list.filter { it.segments.map { it.stops }.equals(filter.map { it.stops }) }
如果我正确理解你的问题,这里有一个 Kotlin 解决方案
fun main() {
// DATA SETUP
val s1 = Segment(1)
val s2 = Segment(2)
val s3 = Segment(3)
val segmentList = listOf(s1,s2,s3)
val query = FlightRecommendationQuery(segmentList)
val f1 = Filter(1)
val f2 = Filter(9999)
val f3 = Filter(3)
val filterList = listOf(f1,f2,f3)
// END OF DATA SETUP
// Here we do the filtration
val filteredSegments = mutableListOf<Segment>()
for(segmentIndex in query.segments.indices) {
// We use indices concept from Kotlin to get the items in the same
// position from both lists
val segment = query.segments[segmentIndex]
val filter = filterList[segmentIndex]
if(segment.stops == filter.stops) filteredSegments.add(segment)
}
//Filtered segments now contains the necessary data
//Will print [Segment(stops=1), Segment(stops=3)] in this case
//Use this list of segments to create a new FlightRecommendationQuery
println(filteredSegments)
}
你可以做到这一点 zipping segments
and filter
and checking if all 他们是平等的:
list.filter { query ->
(query.segments zip filter)
.all { it.first.stops == it.second.stops }
}
另一种方式:
list.filter { query ->
filter.indices.all { filter[it].stops == query.segments[it].stops }
}
all
returns 如果列表的所有元素都匹配给定的谓词则为真
我有一个这样的列表:
val list = List<FlightRecommendationQuery>
在那:
data class FlightRecommendationQuery(
val segments: List<Segment>
)
data class Segment(
val stops: Int?
)
我有另一个列表,其大小与 FlightRecommendationQuery 中的段大小相同,称为过滤器:
val filter = List<Filter>
data class Filter(
val stops: Int?
)
我想过滤 'list' 当过滤器的停止点等于列表中的每个段时。这里,FlightRecommendationQuery 的列表 'filter' 的大小和 'segments' 的大小相同。
更新:我总结了这个解决方案:
list.filter{query->
filter.indices.all{
if(filter[it].stops==null) true
else
filter[it].stops==query.segments[it].stops
}
}
我不确定是否有更有效的方法,但我认为可以这样做:
val filteredList = list.filter { it.segments.map { it.stops }.equals(filter.map { it.stops }) }
如果我正确理解你的问题,这里有一个 Kotlin 解决方案
fun main() {
// DATA SETUP
val s1 = Segment(1)
val s2 = Segment(2)
val s3 = Segment(3)
val segmentList = listOf(s1,s2,s3)
val query = FlightRecommendationQuery(segmentList)
val f1 = Filter(1)
val f2 = Filter(9999)
val f3 = Filter(3)
val filterList = listOf(f1,f2,f3)
// END OF DATA SETUP
// Here we do the filtration
val filteredSegments = mutableListOf<Segment>()
for(segmentIndex in query.segments.indices) {
// We use indices concept from Kotlin to get the items in the same
// position from both lists
val segment = query.segments[segmentIndex]
val filter = filterList[segmentIndex]
if(segment.stops == filter.stops) filteredSegments.add(segment)
}
//Filtered segments now contains the necessary data
//Will print [Segment(stops=1), Segment(stops=3)] in this case
//Use this list of segments to create a new FlightRecommendationQuery
println(filteredSegments)
}
你可以做到这一点 zipping segments
and filter
and checking if all 他们是平等的:
list.filter { query ->
(query.segments zip filter)
.all { it.first.stops == it.second.stops }
}
另一种方式:
list.filter { query ->
filter.indices.all { filter[it].stops == query.segments[it].stops }
}
all
returns 如果列表的所有元素都匹配给定的谓词则为真