Scala SortedMap - iteratorFrom & co 缺少对应项?
Scala SortedMap - iteratorFrom & co missing counterpart?
检索属于更大键的条目
我最近偶然发现了一个 SO question asking how to retrieve keys greater than a given key in a SortedMap
. AfaIk, these SortedMap
值得注意的方法:
from(from: A): SortedMap[A, B]
Creates a ranged projection of this collection with no upper-bound
iteratorFrom(start: A): Iterator[(A, B)]
Creates an iterator over all the key/value pairs contained in this map having a key greater than or equal to start according to the ordering of this map. x.iteratorFrom(y) is equivalent to but often more efficient than x.from(y).iterator.
keysIteratorFrom(start: A): Iterator[A]
Creates an iterator over all the keys(or elements) contained in this collection greater than or equal to start according to the ordering of this collection. x.keysIteratorFrom(y) is equivalent to but often more efficient than x.from(y).keysIterator.
valuesIteratorFrom(start: A): Iterator[B]
Creates an iterator over all the values contained in this map that are associated with a key greater than or equal to start according to the ordering of this map. x.valuesIteratorFrom(y) is equivalent to but often more efficient than x.from(y).valuesIterator.
检索属于较小键的条目
为了检索小于给定键的键,您可以使用这些 SortedMap
方法 (afaIk):
to(to: A): SortedMap[A, B]
Create a range projection of this collection with no lower-bound
问题:
为什么没有模拟方法 iteratorTo
、keysIteratorTo
和 valuesIteratorTo
?
如果它们存在,它们将如何工作?:
- 他们会从最高键向后迭代到最低键吗? (这对我来说更有意义)
- 或者他们会从最低调向前迭代到最高调吗?
在这种情况下,您可以通过执行类似 sortedMap.to(_).iterator()
的操作来获取这些迭代器,尽管 SortedMap 的实现可以提供 iteratorTo
& co(类似于 iteratorFrom
& co)[=63= 的更高效的实现]
开始固定在最低键,takeWhile 就足够了:
scala> val m = TreeMap((0 to 10) map (i => (i, ('a' + i).toChar)) : _*)
m: scala.collection.immutable.TreeMap[Int,Char] = Map(0 -> a, 1 -> b, 2 -> c, 3 -> d, 4 -> e, 5 -> f, 6 -> g, 7 -> h, 8 -> i, 9 -> j, 10 -> k)
scala> m.iterator.takeWhile(_._1 < 5)
res1: Iterator[(Int, Char)] = non-empty iterator
scala> .toList
res2: List[(Int, Char)] = List((0,a), (1,b), (2,c), (3,d), (4,e))
检索属于更大键的条目
我最近偶然发现了一个 SO question asking how to retrieve keys greater than a given key in a SortedMap
. AfaIk, these SortedMap
值得注意的方法:
from(from: A): SortedMap[A, B]
Creates a ranged projection of this collection with no upper-bound
iteratorFrom(start: A): Iterator[(A, B)]
Creates an iterator over all the key/value pairs contained in this map having a key greater than or equal to start according to the ordering of this map. x.iteratorFrom(y) is equivalent to but often more efficient than x.from(y).iterator.
keysIteratorFrom(start: A): Iterator[A]
Creates an iterator over all the keys(or elements) contained in this collection greater than or equal to start according to the ordering of this collection. x.keysIteratorFrom(y) is equivalent to but often more efficient than x.from(y).keysIterator.
valuesIteratorFrom(start: A): Iterator[B]
Creates an iterator over all the values contained in this map that are associated with a key greater than or equal to start according to the ordering of this map. x.valuesIteratorFrom(y) is equivalent to but often more efficient than x.from(y).valuesIterator.
检索属于较小键的条目
为了检索小于给定键的键,您可以使用这些 SortedMap
方法 (afaIk):
to(to: A): SortedMap[A, B]
Create a range projection of this collection with no lower-bound
问题:
为什么没有模拟方法 iteratorTo
、keysIteratorTo
和 valuesIteratorTo
?
如果它们存在,它们将如何工作?:
- 他们会从最高键向后迭代到最低键吗? (这对我来说更有意义)
- 或者他们会从最低调向前迭代到最高调吗?
在这种情况下,您可以通过执行类似sortedMap.to(_).iterator()
的操作来获取这些迭代器,尽管 SortedMap 的实现可以提供iteratorTo
& co(类似于iteratorFrom
& co)[=63= 的更高效的实现]
开始固定在最低键,takeWhile 就足够了:
scala> val m = TreeMap((0 to 10) map (i => (i, ('a' + i).toChar)) : _*)
m: scala.collection.immutable.TreeMap[Int,Char] = Map(0 -> a, 1 -> b, 2 -> c, 3 -> d, 4 -> e, 5 -> f, 6 -> g, 7 -> h, 8 -> i, 9 -> j, 10 -> k)
scala> m.iterator.takeWhile(_._1 < 5)
res1: Iterator[(Int, Char)] = non-empty iterator
scala> .toList
res2: List[(Int, Char)] = List((0,a), (1,b), (2,c), (3,d), (4,e))