如何在Java中使用SortedMap接口或TreeMap?
我有一个
Map<Float, MyObject>
根据浮动对地图进行排序的最佳方法是什么?
SortedMap
或 TreeMap
是最好的答案吗?
我该如何使用它?
我只创建一次地图,并经常使用 myMap.put()
和 myMap.get()
替换 MyObject
。
I have a
Map<Float, MyObject>
What is the best way to keep the map sorted according to the float?
Is SortedMap
or TreeMap
the best answer?
How do I use it?
I only create the map once and replace the MyObject
frequently using myMap.put()
and myMap.get()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我会使用
TreeMap
,它实现SortedMap
。它正是为此而设计的。示例:
请参阅SortedMap 的 Java 教程页面。< br>
这里有与 TreeMap 相关的教程列表。
I would use
TreeMap
, which implementsSortedMap
. It is designed exactly for that.Example:
See the Java tutorial page for SortedMap.
And here a list of tutorials related to TreeMap.
TreeMap 可能是最直接的方法。您可以像使用普通地图一样使用它。即
值得一看 API 文档, http: //download.oracle.com/javase/6/docs/api/java/util/TreeMap.html 看看您还可以用它做什么。
A TreeMap is probably the most straightforward way of doing this. You use it exactly like a normal Map. i.e.
It's worth taking a look at the API docs, http://download.oracle.com/javase/6/docs/api/java/util/TreeMap.html to see what else you can do with it.
您可以使用 TreeMap,它在内部实现 SortedMap,下面是示例
按升序排序:
按降序排序:
如果您始终希望创建地图以使用降序排列一般来说,如果您只需要一次,请创建一个降序排列的 TreeMap 并将原始映射中的所有数据放入其中。
有关 SortedMAP 的更多信息,请阅读 http://examples.javacodegeeks.com/core-java/ util/treemap/java-sorted-map-example/
You can use TreeMap which internally implements the SortedMap below is the example
Sorting by ascending ordering :
Sorting by descending ordering :
If you always want this create the map to use descending order in general, if you only need it once create a TreeMap with descending order and put all the data from the original map in.
for further information about SortedMAP read http://examples.javacodegeeks.com/core-java/util/treemap/java-sorted-map-example/
TreeMap(它是 SortedMap 接口的实现)可以工作。
TreeMap, which is an implementation of the SortedMap interface, would work.
tl;dr
使用
Map
与 Java 6 及更高版本捆绑在一起的实现NavigableMap
(
SortedMap
):TreeMap
如果运行单线程,或者如果映射在第一个之后跨线程只读正在有人居住。NavigableMap
仅供参考,
SortedMap
接口由NavigableMap
接口。
如果使用尚未声明支持
NavigableMap
的第 3 方实现,则只需使用SortedMap
。在与 Java 捆绑的地图中,两个实现SortedMap
的实现也实现NavigableMap
。接口与具体类
,而
TreeMap
是该接口的多个实现之一(以及更新的NavigableMap
)。正如其他人提到的,
SortedMap
是一个接口 如果您以后决定在实现之间切换,则可以编写使用该映射的代码,如果以后更改实现,则该代码仍然有效。 将该声明更改为:
...和。使用该映射的其余代码继续 Java 11 捆绑了十种
Map
实现。选择实现
还有第三方提供的更多实现,例如 Google Guava。
这是我制作的一个图表,突出显示了每个功能的各种功能。请注意,两个捆绑实现通过检查键的内容来保持键的排序顺序。此外,
EnumMap
按照该枚举上定义的对象的顺序保留其键。最后,LinkedHashMap 会记住原始插入顺序。tl;dr
Use either of the
Map
implementations bundled with Java 6 and later that implementNavigableMap
(the successor toSortedMap
):TreeMap
if running single-threaded, or if the map is to be read-only across threads after first being populated.ConcurrentSkipListMap
if manipulating the map across threads.NavigableMap
FYI, the
SortedMap
interface was succeeded by theNavigableMap
interface.You would only need to use
SortedMap
if using 3rd-party implementations that have not yet declared their support ofNavigableMap
. Of the maps bundled with Java, both of the implementations that implementSortedMap
also implementNavigableMap
.Interface versus concrete class
As others mentioned,
SortedMap
is an interface whileTreeMap
is one of multiple implementations of that interface (and of the more recentNavigableMap
.Having an interface allows you to write code that uses the map without breaking if you later decide to switch between implementations.
This code still works if later change implementations. Perhaps you later need a map that supports concurrency for use across threads. Change that declaration to:
…and the rest of your code using that map continues to work.
Choosing implementation
There are ten implementations of
Map
bundled with Java 11. And more implementations provided by 3rd parties such as Google Guava.Here is a graphic table I made highlighting the various features of each. Notice that two of the bundled implementations keep the keys in sorted order by examining the key’s content. Also,
EnumMap
keeps its keys in the order of the objects defined on that enum. Lastly, theLinkedHashMap
remembers original insertion order.TreeMap
按键自然顺序排序。这些键应该实现Comparable
或与Comparator
兼容(如果您将一个实例传递给构造函数)。在您的情况下,Float
已经实现了Comparable
,因此您无需执行任何特殊操作。您可以调用
keySet
来按升序检索所有键。TreeMap
sorts by the key natural ordering. The keys should implementComparable
or be compatible with aComparator
(if you passed one instance to constructor). In you case,Float
already implementsComparable
so you don't have to do anything special.You can call
keySet
to retrieve all the keys in ascending order.您可以使用实现
SortedMap
接口的TreeMap
类来实现此目的。TreeMap
类实现了NavigableMap
接口,该接口扩展了SortedMap
接口。这将产生以下效果:所有键都将根据其
Comparable
接口的实现进行排序。键现在会自动按排序顺序排列。当我们迭代它们时,我们得到以下结果:
因为
TreeMap
元素需要排序,所以用作键的对象需要具有Comparable
接口的实现。例如,以下代码将引发运行时错误:You can use the
TreeMap
class for this purpose which implements theSortedMap
interface.The class
TreeMap
implements theNavigableMap
interface which extends theSortedMap
interface.This will have the effect that all the keys will be ordered according to their implementation of their
Comparable
interface.The keys are now automatically in sorted order. When we iterate over them we get the following:
Because
TreeMap
elements are required to be ordered the Objects which are used as a key need to have a implementation of theComparable
interface. For example the following code will throw a runtime error: