我正在尝试通过
实现一个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/