将Java TreeMap代码迁移到Scala? [英] Migrating Java TreeMap code to Scala?
问题描述
我正在将我的Java代码库迁移到纯Scala,而且我被关于这一段代码。我有一个IntervalMap的实现,即一个数据结构,让你有效地将范围 [从,到]
映射到值
其中设置
,删除
和获取
操作全部 O(log n)
(与IntervalTree或SegmentTree略有不同)。
I am migrating my Java code base to pure Scala and I am stuck on this one piece of code. I have an implementation of an IntervalMap i.e. a data structures that let's you efficiently map ranges [from,to]
to values
where the set
, delete
and get
operations are all O(log n)
(slightly different from an IntervalTree or a SegmentTree).
此代码使用Java的 java.util.TreeMaps
在迁移到Scala时,我遇到了两个大问题:
This code uses Java's java.util.TreeMaps
and while migrating to Scala, I ran into 2 big issues:
-
Scala没有
mutable.TreeMap
- 我决定使用mutable.TreeSet
来解决它(奇怪的是Scala)有mutable.TreeSet
但没有mutable.TreeMap
)用于存储密钥并将值存储在辅助<$ c中$ C> mutable.Map 。这是一个令人不快的黑客,但还有更好的方法吗?
Scala has no
mutable.TreeMap
- I decided to go around it by usingmutable.TreeSet
(oddly Scala hasmutable.TreeSet
but nomutable.TreeMap
) for storing the keys and storing the values in an auxiliarymutable.Map
. This is an unpleasant hack but is there any better way?
下一个问题是Scala的 mutable.TreeSet
没有相当于 java.util.TreeSet
的 ceilingKey
, floorEntry
, pollFirst
, pollLast
这些都是 O(log n)
Java中的操作。
Next problem is Scala's mutable.TreeSet
has no equivalent of java.util.TreeSet
's ceilingKey
, floorEntry
, pollFirst
, pollLast
which are all O(log n)
operations in Java.
那么,我怎样才能最好地将我的代码迁移到Scala?这些情况下的最佳做法是什么?我真的不想编写自己的树实现。有没有更惯用的Scala编写IntervalMaps的方式,我不知道?或者那里有一些有信誉的图书馆?或者Scala只是简单地使用它的gimped TreeSet和不存在的TreeMaps来吮吸它。当然,我可以在Scala中使用Java的 TreeMap
但这很难看,我失去了所有不错的Scala集合功能,我当时也可以使用Java。
So, how can I best migrate my code to Scala? What are the best practices in these situations? I really do not want to write my own tree implementations. Is there a more idiomatic Scala way of writing IntervalMaps that I am not aware of? Or is there some reputable library out there? Or does Scala just plain suck here with its gimped TreeSet and non-existent TreeMaps. Ofcourse I can just use Java's TreeMap
in Scala but that is ugly and I lose all the nice Scala collection features and I might as well use Java then.
这是我目前的Java代码: https://gist.github.com/pathikrit/5574521
Here is my current Java code: https://gist.github.com/pathikrit/5574521
推荐答案
Scala 2.12有 mutable.TreeMap
最后:< a href =https://github.com/scala/scala/pull/4504\"rel =nofollow> https://github.com/scala/scala/pull/4504
Scala 2.12 has mutable.TreeMap
finally: https://github.com/scala/scala/pull/4504
这篇关于将Java TreeMap代码迁移到Scala?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!