如果我插入和访问订单,我应该使用linkedhashmap或treemap吗?

发布于 2025-01-23 06:16:00 字数 180 浏览 4 评论 0 原文

如果我以自然顺序进行并迭代,是否应该使用 linkedhashmap treemap

我将使用 map< int,myobj> ,然后以自然顺序(1,2,3,...)放置它们。 我知道他们的大型时间表演,但我也知道这是一个边界的使用。

If i put and iterate in natural order, should I use LinkedHashMap or TreeMap?

I will use a Map<int, MyObj>, and I will put them in natural order (1,2,3,...).
I know about their big-O time performances, but I also know that this is a borderline use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离鸿 2025-01-30 06:16:00

使用 treemap 。不是出于任何性能原因,而是因为可以将其分配给 sortedMap (或 navigablemap ),并且可以清楚地传达您的意图,即该地图具有定义的顺序。

Use a TreeMap. Not for any performance reasons, but because it can be assigned to SortedMap (or NavigableMap), and that communicates clearly your intent that the map has a defined order.

冰魂雪魄 2025-01-30 06:16:00
  • 如果要以最初插入的顺序保留条目,请使用 linkedhashmap
  • 如果要按顺序排序保存的条目,请使用a treemap concurrenthashmap 。后者是线程安全。

默认情况下的排序地图自然顺序。可选地,您可以提供 比较器 用于排序。这已经在堆栈溢出上已经涵盖了很多次,因此请搜索以了解更多信息。

  • If you want to keep the entries in the order in which you originally insert them, use LinkedHashMap.
  • If you want the entries kept in a sorted order, use a NavigableMap, the successor to SortedMap. Java comes with two such implementations: TreeMap and ConcurrentHashMap. The latter is thread-safe.

The sorted maps by default use natural order. Optionally, you can provide a Comparator to use for sorting. This has been covered many times already on Stack Overflow, so search to learn more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文