开发者

Selecting keys of a TreeMap by their index

Imagine you have the following TreeMap:

var dates = new TreeMap[Long, Tuple2[Int, Double]]()

I know I can loop through it with:

dates.foreach { case (date, (id, rotation)) => 
   ...          
}

But in my code, this loop takes place within another loop and I would therefore like to advance myself in the dates keys, typically with a currIndex : Int variable that I would increment according to a condition.

I thought one could do something like:

date = dates.keys(currIndex)

but it doesn't look like this is possib开发者_JAVA技巧le... any idea how to do that?


Edit: trying to address your comment:

You can convert the whole keys to an IndexedSeq beforehand:

val keysSeq = dates.keySet.toIndexedSeq

// later, obtain an index
val index: Int = /* ... */
// lookup the key
val (valueInt, valueDouble) = dates(keysSeq(index))

Previous answer

You could try something like this:

dates.iterator.zipWithIndex.foreach {
  case ((key, (valueInt, valueDouble)), index) =>

}

Would that work for you? I'm not sure I properly understand your requirement of “increment[ing currIndex] according to a condition”…

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜