Java中可排序的类似HashMap的数据结构? [英] Sortable HashMap-like data structure in Java?

查看:270
本文介绍了Java中可排序的类似HashMap的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中是否有类似于可以按键或值排序的HashMap的某种数据结构?在PHP中,您可以使用可排序的关联数组。 Java有这样的事吗?

Is there some sort of data structure in Java that resembles a HashMap that can be sorted by key or value? In PHP you can have associative arrays that are sortable. Is there such a thing in Java?

推荐答案

HashMap 一个很好的散列函数会产生看似随机分配的密钥。

HashMaps are unsorted almost by definition; a good hash function will produce a seemingly random distribution of the keys.

如果你想使用 Map 考虑查找 TreeMap ,它由排序的二叉搜索树支持。

If you want to use a Map in Java that stores its elements in sorted order, consider looking into TreeMap, which is backed by a sorted binary search tree.

如果你想要一些按键或按值分类的东西,你可能正在寻找双向地图或bimap。 Java在标准库中没有,而我所了解的最接近的实现是 Google的 BiMap 。但是,正如潘加指出的那样,它并不支持排序顺序的元素。您可以通过使用两个 TreeMap 来轻松实现自己的实现,但从键到值中的一个,另一个从值到键。

If you want something that can be sorted either by key or by value, you may be looking for a bidirectional map or "bimap." Java doesn't have on in its standard libraries, and the closest implementation I know of is Google's BiMap. However, as Pangea pointed out, it does not support elements in sorted order. You could easily make your own implementation by just using two TreeMaps, though, one from keys to values and one from values to keys.

希望这有帮助!

这篇关于Java中可排序的类似HashMap的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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