[英]How do you declare a TreeMap - SortedMap or Map?
I have written some code that works fine but I am confused about the correct way to declare a TreeMap. 我写了一些可以正常工作的代码,但是对于声明TreeMap的正确方法感到困惑。
If SortedMap is a subinterface of Map then is it okay to just use Map if the code is working okay? 如果SortedMap是Map的子接口,那么在代码可以正常工作的情况下只使用Map是可以的吗? Is SortedMap even necessary if TreeMap works fine with Map?
如果TreeMap与Map兼容,是否还需要SortedMap?
Should it be: 应该是:
private Map<String, List <Bus>> map = new TreeMap<String, List <Bus>>();
or 要么
private SortedMap<String, List <Bus>> map = new TreeMap<String, List <Bus>>();
Thanks. 谢谢。 Sorry this is so basic - I am new to Java.
抱歉,这是如此基础-我是Java新手。
I've used SortedMap
to inform others that it is already sorted. 我已经使用
SortedMap
通知其他人它已经被排序了。 Using Map
is OK too. 使用
Map
也可以。
private Map<String, List <Bus>> busTimetable = new TreeMap<String, List <Bus>>();
除非您有充分的理由,否则请始终使用最高级别的界面。
The answer to your question depends on your usage. 您问题的答案取决于您的用法。 By default, you should simply program to the data type's interface (ie
Map
). 默认情况下,您应该只编程到数据类型的接口(即
Map
)。 If SortedMap
provides methods that you will be using that aren't declared in Map
, then program to SortedMap
. 如果
SortedMap
提供了Map
中未声明的将要使用的方法,则对SortedMap
进行编程。
It depends upon your requirement and design whenever possible use the highest level of abstraction that is Map. 这取决于您的要求和设计,只要有可能,请使用Map的最高抽象级别。 The reason is say you are creating a service and it consume list of data and produce an output in a Map.
原因是您正在创建服务,并且该服务使用数据列表并在Map中产生输出。 some client may expect for sorted order of the data in the map and some other client may just need the data in the insertion order of the map if you use a specific interface SortedMap ;
如果使用特定的接口SortedMap,则某些客户端可能期望映射中数据的排序顺序,而另一些客户端可能仅需要映射中插入顺序的数据; this kind of scenarion you can not handle using one service and you will end up creating two different api becuase one who is expecting sorted order you can just return an implementaion of TreeMap and one for insertion order you can use LinkedHashMap.
这种情况下,您将无法使用一种服务来处理,最终将创建两个不同的api,因为一个期望排序的API可以返回TreeMap的实现,而另一个可以使用LinkedHashMap的插入命令。 So it's about how flexible is your program.
因此,这关系到您的程序有多灵活。
If you need to use specific SortedMap
methods (like firstKey()
/ lastKey()
/whatever...), it is mandatory to declare your reference as SortedMap
. 如果您需要使用特定的
SortedMap
方法(例如firstKey()
/ lastKey()
/ whatever ...),则必须将您的引用声明为SortedMap
。 Otherwise, Map
is the one I'd choose, if I'm only planning on using it as a Map
, so I'll be able so switch implementations without any other change in the code. 否则,
Map
是一个我会选择,如果我只用它作为规划的Map
,所以我就可以使开关实现无代码中的任何其他变化。
I agree with other commenters that you use SortedMap
if you use methods that aren't in vanilla Map
. 我同意其他评论者的意见,如果您使用的不是Vanilla
Map
方法,则可以使用SortedMap
。 Also use SortedMap
if you have use in an iterator or a for-each loop if they implicitly rely on sorted input. 如果已在迭代器中使用,也请使用
SortedMap
如果它们隐式依赖于已排序的输入,则也可以使用for-each循环。
If neither of these cases are true, you should also think about if you only need a vanilla Map
, a HashMap
may be a better choice. 如果以上两种情况都不成立,则还应该考虑是否仅需要普通
Map
,而HashMap
可能是更好的选择。 HashMap
has O(1) access; HashMap
具有O(1)访问权限; TreeMap
does not. TreeMap
没有。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.