HashMap和TreeMap有什么区别? [英] What is the difference between a HashMap and a TreeMap?

查看:188
本文介绍了HashMap和TreeMap有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Java。我什么时候在TreeMap上使用HashMap?

I started learning Java. When would I use a HashMap over a TreeMap?

推荐答案

TreeMap SortedMap ,这意味着可以对键的顺序进行排序,当迭代键时,您可以预期它们将按顺序排列。

TreeMap is an example of a SortedMap, which means that the order of the keys can be sorted, and when iterating over the keys, you can expect that they will be in order.

HashMap 另一方面,没有这样的保证。因此,当迭代 HashMap 的键时,您无法确定它们的顺序。

HashMap on the other hand, makes no such guarantee. Therefore, when iterating over the keys of a HashMap, you can't be sure what order they will be in.

HashMap 通常会更有效率,所以每当你不关心键的顺序时就使用它。

HashMap will be more efficient in general, so use it whenever you don't care about the order of the keys.

这篇关于HashMap和TreeMap有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆