java - 哈希表数据排序

标签 java hashtable

我正在尝试通过

实现一个HashTable
HashTable ht=new HashTable(); 

我在其中通过一个 xml 文件为其赋值。

问题是,当我打印哈希表的值时,它不是以相同的顺序出现,而是按升序排列。

假设:- 如果键是 [US, AU, CA] 并具有相应的值,那么我试图按照它们在文件 (xml) 中出现的相同顺序获取它们,但它会像 [AU, CA] , US] 具有相应的值。

如有任何帮助,我们将不胜感激。

问候, 阿尔沙德。

最佳答案

The problem is that when i am printing the value of hashtable its not coming in the same order, Its taking ascending order.

不一定 - 它会以某种未指定的顺序排列。您必须依赖Hashtable 的顺序。

如果您想要保留插入顺序的 map ,请使用 LinkedHashMap . (您几乎可以肯定首先不想要 Hashtable,而是想要 HashMap。)

documentation 添加关于 HashMap 中元素的排序:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

关于java - 哈希表数据排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11133067/

相关文章:

java - 你能排除一个对象不被 Java 中的垃圾收集器跟踪吗?

java - 为什么 getClass 返回一个 Class<?扩展 |X|>?

python - 在Python中用 "gaps"模拟一个数组

Java如何不断重绘

java - 从链表中剪切一部分?

java - 返回 for 循环外部的 for 循环元素

java - 寻找有效的方法将哈希表的元素提取到单个数组中

java - 用java可视化HashTable

java:通过哈希函数内存构造

c - 具有特定要求的纯C通用双向哈希表