简体   繁体   English

如何对Java Hashtable进行排序?

[英]How to sort a Java Hashtable?

I inserted some data into a Java Hashtable. 我将一些数据插入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? 如果我从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()
    );
}

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. Map接口的哈希表和链表实现,具有可预测的迭代顺序。 This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. 此实现与HashMap的不同之处在于它维护了一个贯穿其所有条目的双向链表。 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 map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.) (A键k被重新插入到地图m如果m.put(k, v)被调用时m.containsKey(k)将在调用之前立即返回true。)

This implementation spares its clients from the unspecified, generally chaotic ordering provided by HashMap (and Hashtable ), without incurring the increased cost associated with TreeMap . 此实现使其客户端免受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 ; 请注意,这通常与HashMap而不是Hashtable - 我不知道保留的顺序等同于Hashtable ; the latter isn't usually used these days anyway (just as ArrayList is usually used in preference to Vector ). 这些天通常不会使用后者(正如ArrayList通常优先使用Vector )。

I've assumed you want insertion order rather than key-sorted order. 我假设你想要插入顺序而不是按键排序的顺序。 If you want the latter, use TreeMap . 如果您想要后者,请使用TreeMap

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. 虽然Hashtable无法排序,但他询问如何获取排序数据,可以对从HashTable提取的密钥列表进行排序并按顺序检索值。 Something like: 就像是:

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. 会没事的。

A Hashtable has no predictable iteration order, and cannot be sorted. Hashtable没有可预测的迭代顺序,无法排序。 If you only want predictable iteration order you should use a LinkedHashMap . 如果您只想要可预测的迭代顺序,则应使用LinkedHashMap If you want to be able to sort your Map , you should use a TreeMap . 如果您希望能够对Map进行排序,则应使用TreeMap

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);

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集合取代。我建议你避免它,以及VectorEnumeration

Instead of Hashtable use HashMap where possible. 而不是Hashtable尽可能使用HashMap You can add synchronization using Collections.synchronizedMap(map) if you need it. 如果需要,可以使用Collections.synchronizedMap(map)添加同步。

Instead of Vector , use ArrayList where possible. 而不是Vector ,尽可能使用ArrayList You can add synchronization using Collections.synchronizedList(map) if you need it. 如果需要,可以使用Collections.synchronizedList(map)添加同步。

Instead of Enumeration you can use Iterator or even a for-each loop. 您可以使用Iterator甚至for-each循环代替Enumeration

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).. 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 (eg a subclass of java.util.List). 如果要按特定顺序存储元素,则需要使用列表(例如java.util.List的子类)。

And BTW, your code sample does not even contain a hash table. 而顺便说一句,您的代码示例甚至不包含哈希表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM