如何对 Java Hashtable 进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4333265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to sort a Java Hashtable?
提问by Tester
I inserted some data into a Java Hashtable. If I read the data from the Hashtable it doesn't come back in the same order that I inserted it in. How do I get the ordered data from the Hashtable?
我将一些数据插入到 Java 哈希表中。如果我从 Hashtable 中读取数据,它不会按照我插入的顺序返回。如何从 Hashtable 中获取有序数据?
I use the following code to get the values from the hashtable:
我使用以下代码从哈希表中获取值:
// Get a set of the entries
Set set = hsUpdateValues.entrySet();
// Get an iterator
Iterator i = set.iterator();
// Display elements
while (i.hasNext()) {
Map.Entry me = (Map.Entry) i.next();
System.out.print(
"Key : " + me.getKey()
+ ", Value: " + me.getValue()
);
}
采纳答案by Jon Skeet
If you want an order-preserving map, you should use LinkedHashMap
:
如果你想要一个保留顺序的地图,你应该使用LinkedHashMap
:
Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key
k
is reinserted into a mapm
ifm.put(k, v)
is invoked whenm.containsKey(k)
would return true immediately prior to the invocation.)This implementation spares its clients from the unspecified, generally chaotic ordering provided by
HashMap
(andHashtable
), without incurring the increased cost associated withTreeMap
.
Map接口的哈希表和链表实现,迭代顺序可预测。此实现与 HashMap 的不同之处在于它维护一个双向链表,贯穿其所有条目。这个链表定义了迭代顺序,这通常是键被插入到映射中的顺序(插入顺序)。请注意,如果将键重新插入到映射中,则插入顺序不会受到影响。(如果在调用之前立即返回 true时调用,则将键
k
重新插入映射中。)m
m.put(k, v)
m.containsKey(k)
此实现使其客户免于
HashMap
(和Hashtable
)提供的未指定的、通常混乱的排序,而不会导致与TreeMap
.
Note that this is usually compared with HashMap
rather than Hashtable
- I don't know of an order-preserving equivalent to Hashtable
; the latter isn't usually used these days anyway (just as ArrayList
is usually used in preference to Vector
).
请注意,这通常与HashMap
而不是进行比较Hashtable
- 我不知道与Hashtable
; 无论如何,后者现在通常不使用(就像ArrayList
通常优先使用Vector
)。
I've assumed you want insertionorder rather than key-sortedorder. If you want the latter, use TreeMap
.
我假设您想要插入顺序而不是键排序顺序。如果您想要后者,请使用TreeMap
.
回答by sleske
If i read the data from the hash table it's not coming in the same order what i inserted.
如果我从哈希表中读取数据,它的顺序与我插入的顺序不同。
Your question does not make sense. A Hashtable does not have an "order", it is unordered (Edit: Some implementations do have an order, but it's not common for a hashtable)..
你的问题没有意义。哈希表没有“顺序”,它是无序的(编辑:某些实现确实有顺序,但对于哈希表来说并不常见)。
In what order would you expect the entries to be?
您希望条目按什么顺序排列?
If you want to store elements in a certain order, you need to use a list (e.g. a subclass of java.util.List).
如果你想以某种顺序存储元素,你需要使用一个列表(例如 java.util.List 的子类)。
And BTW, your code sample does not even contain a hash table.
顺便说一句,您的代码示例甚至不包含哈希表。
回答by Mohamed Saligh
Use TreeMap for sorting it:
使用 TreeMap 对其进行排序:
Map<String, String> yourMap = new HashMap<String, String>();
yourMap.put("1", "one");
yourMap.put("2", "two");
yourMap.put("3", "three");
Map<String, String> sortedMap = new TreeMap<String, String>(yourMap);
回答by Bj?rn
A Hashtable
has no predictable iteration order, and cannot be sorted. If you only want predictable iteration order you should use a LinkedHashMap
. If you want to be able to sort your Map
, you should use a TreeMap
.
AHashtable
没有可预测的迭代顺序,并且无法排序。如果您只想要可预测的迭代顺序,您应该使用LinkedHashMap
. 如果您希望能够对您的 进行排序Map
,您应该使用TreeMap
.
回答by Peter Lawrey
Hashtable
is a legacy collection which was replaced by Java 1.2 collections in 1998. I suggest you avoid it, along with Vector
and Enumeration
.
Hashtable
是一个遗留集合,在 1998 年被 Java 1.2 集合取代。我建议你避免它,以及Vector
和Enumeration
。
Instead of Hashtable
use HashMap
where possible. You can add synchronization using Collections.synchronizedMap(map)
if you need it.
而不是在可能的情况下Hashtable
使用HashMap
。Collections.synchronizedMap(map)
如果需要,您可以使用添加同步。
Instead of Vector
, use ArrayList
where possible. You can add synchronization using Collections.synchronizedList(map)
if you need it.
代替Vector
,ArrayList
在可能的情况下使用。Collections.synchronizedList(map)
如果需要,您可以使用添加同步。
Instead of Enumeration
you can use Iterator
or even a for-each
loop.
而不是Enumeration
你可以使用Iterator
甚至for-each
循环。
回答by Filippo Mazza
Although a Hashtable
cannot be sorted, he asked how to get sorted data, that can be done sorting the list of keys extracted from the HashTable
and retrieving values in that order.
Something like:
尽管 aHashtable
无法排序,但他询问如何获取已排序的数据,这可以通过对从 a 中提取的键列表进行排序HashTable
并按该顺序检索值来完成。就像是:
List<'your_type'> tmp = Collections.list('your_hashtable'.keys());
Collections.sort(tmp);
Iterator<'your_type'> it = tmp.iterator();
while(it.hasNext()){
'your_type' element =it.next();
//here you can get ordered things: 'your_hashtable'.get(element);
}
will be fine.
会没事的。