Reputation: 11025
Having a case that a map is passed in with data like
Map<Pair<Int, Int>), String>
Pair<Int, Int>) // zero based star index, inclusive end index
to
displayString
when doing the process, need to pick up proper displayString for current position, like
var index = 0
for (data in dataList) {
/*
if the current index is within one of the pair<int, int>
then use the mapped displayString
*/
index++
}
what is best way to do in kotlin?
Upvotes: 9
Views: 15220
Reputation: 9404
Not sure if this is what you meant, but possibly something like this:
var index = 0
for (data in dataList) {
/*
if the current index is within one of the pair<int, int>
then use the mapped displayString
*/
val currentPair = data.key
if ((currentPair.first .. currentPair.second).contains(index)) {
// do something
}
index++
}
Though if you meant you wanted a getter to get the first pair from the map which contains an int the perhaps you wanted something like this:
fun getByIndex(dataList: Map<Pair<Int,Int>, String>, index: Int): String? {
return dataList.firstOrNull {
(it.key.first .. it.key.second).contains(index)
}
}
Use until
instead of ..
to have exclusive end index
Upvotes: 9