📜  如何在Java中对 Hashtable 进行排序?

📅  最后修改于: 2022-05-13 01:55:29.733000             🧑  作者: Mango

如何在Java中对 Hashtable 进行排序?

给定一个 Hashtable,任务是对这个 Hashtable 进行排序。哈希表是一种以键值格式存储数据的数据结构。存储的数据既不按排序顺序,也不保留插入顺序。

例子

Java
import java.io.*;
import java.util.*;
  
public class SortHashtable {
    public static void main(String[] args)
    {
  
        // create a hashtable
        Hashtable ht
            = new Hashtable();
  
        // insert data into hashtable
        ht.put(2, "mango");
        ht.put(3, "orange");
        ht.put(1, "apple");
  
        Set keys = ht.keySet();
        Iterator itr = keys.iterator();
  
        // traverse the TreeMap using iterator
        while (itr.hasNext()) {
            Integer i = itr.next();
            System.out.println(i + " " + ht.get(i));
        }
    }
}


Java
import java.io.*;
import java.util.*;
  
public class SortHashtable {
    public static void main(String[] args)
    {
  
        // create a hashtable
        Hashtable ht
            = new Hashtable();
  
        // insert data into hashtable
        ht.put(2, "mango");
        ht.put(3, "orange");
        ht.put(1, "apple");
  
        // create a TreeMap
        TreeMap tm
            = new TreeMap(ht);
  
        // create a keyset
        Set keys = tm.keySet();
        Iterator itr = keys.iterator();
  
        // traverse the TreeMap using iterator
        while (itr.hasNext()) {
            Integer i = itr.next();
            System.out.println(i + " " + tm.get(i));
        }
    }
}


Java
import java.io.*;
import java.util.*;
  
public class SortHashTable {
    public static void main(String[] args)
    {
  
        // create a LinkedHashMap
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        // insert data into LinkeHashMap
        lhm.put(2, "mango");
        lhm.put(3, "orange");
        lhm.put(1, "apple");
  
        // prepare a keyset
        Set keys = lhm.keySet();
        Iterator itr = keys.iterator();
  
        // traverse the LinkedHashMap using iterator
        while (itr.hasNext()) {
            Integer i = itr.next();
            System.out.println(i + " " + lhm.get(i));
        }
    }
}


输出
3 orange
2 mango
1 apple

可以使用以下两种方式对 Hashtable 映射进行排序:

  1. 使用树图
  2. 使用 LinkedHashMap

例子:

方法一:

TreeMap 按排序顺序存储数据。我们可以使用 TreeMap 构造函数并将 Hashtable 对象转换为 TreeMap 对象。现在生成的 TreeMap 对象已排序。

句法:

TreeMap tm = new TreeMap(Map m);

参数: m是我们程序中的Hashtable。

例子

Java

import java.io.*;
import java.util.*;
  
public class SortHashtable {
    public static void main(String[] args)
    {
  
        // create a hashtable
        Hashtable ht
            = new Hashtable();
  
        // insert data into hashtable
        ht.put(2, "mango");
        ht.put(3, "orange");
        ht.put(1, "apple");
  
        // create a TreeMap
        TreeMap tm
            = new TreeMap(ht);
  
        // create a keyset
        Set keys = tm.keySet();
        Iterator itr = keys.iterator();
  
        // traverse the TreeMap using iterator
        while (itr.hasNext()) {
            Integer i = itr.next();
            System.out.println(i + " " + tm.get(i));
        }
    }
}
输出
1 apple
2 mango
3 orange

方法二:

LinkedHashMap 按照插入的顺序存储数据。当数据到来时,将其插入到 LinkedHashMap 中,它具有保留插入顺序的属性。

句法:

LinkedHashMap lhm = new LinkedHashMap();

例子

Java

import java.io.*;
import java.util.*;
  
public class SortHashTable {
    public static void main(String[] args)
    {
  
        // create a LinkedHashMap
        LinkedHashMap lhm
            = new LinkedHashMap();
  
        // insert data into LinkeHashMap
        lhm.put(2, "mango");
        lhm.put(3, "orange");
        lhm.put(1, "apple");
  
        // prepare a keyset
        Set keys = lhm.keySet();
        Iterator itr = keys.iterator();
  
        // traverse the LinkedHashMap using iterator
        while (itr.hasNext()) {
            Integer i = itr.next();
            System.out.println(i + " " + lhm.get(i));
        }
    }
}
输出
2 mango
3 orange
1 apple