有没有一种简单的方法可以在 Java 中按字母顺序排列字符串枚举?

发布于 2024-08-07 03:29:50 字数 670 浏览 3 评论 0原文

正如标题所说。

我尝试用 List[] 上的 Collections.sort() 和 ArrayList 的 .sort() 函数搞乱一些,但我永远无法将其解析回枚举。

谢谢!

编辑:

这是一些伪代码和进一步的解释。我的目标是从哈希表中获取keys(),并按字母顺序对每个键进行复杂的操作。

我当前的流程是:

  1. 获取一个给定的哈希表
  2. 从 ht 中形成一个枚举
  3. 运行 while 循环,直到枚举为空

所以代码如下:

public void processData(Hashtable<String,GenericClass> htData)
{
    Enumeration<String> enData = htData.keys();

    while(enData.hasMoreElements())
    {
        String current = enData.nextElement();

        /*
         * DO COMPLEX PROCESS
         */
    }
}

问题是枚举中的数据必须按字母顺序排列(也就是说,必须按字母顺序对每个键执行“复杂的过程”)。有什么解决办法吗?谢谢!

Just as the title says.

I tried messing around a bit with Collections.sort() on a List[] and the .sort() function of an ArrayList but I was never able to parse it back to an Enumeration.

Thanks!

EDIT:

Here's some pseduocode and further explanation. My goal is to take the keys() from a Hashtable and do complex operations involving each one, alphabetically.

My current process is:

  1. Take a hash table I'm given
  2. Form an enumeration from the ht
  3. Run a while loop until the enumeration is empty

So the code is like this:

public void processData(Hashtable<String,GenericClass> htData)
{
    Enumeration<String> enData = htData.keys();

    while(enData.hasMoreElements())
    {
        String current = enData.nextElement();

        /*
         * DO COMPLEX PROCESS
         */
    }
}

The problem, is that the data in the enumeration has to be alphabetical (that is, the "complex process" must be done on each key in alphabetical order). Any solutions? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情丝乱 2024-08-14 03:29:50

如果您有一个排序列表,您可以使用

Collections.enumeration(myList)

将其返回到枚举...如果我正确地跟随您...

编辑:

您可以这样做...

List l = new ArrayList(ht.keySet());
Collections.sort(l)

If you have a sorted list you can use

Collections.enumeration(myList)

to get it back to an enumeration... if I'm following you correctly..

EDIT:

You can do this...

List l = new ArrayList(ht.keySet());
Collections.sort(l)
林空鹿饮溪 2024-08-14 03:29:50

如果您需要按键的顺序迭代项目,也许最好使用 TreeMap 而不是 Hashtable。它的 keySet 方法将按顺序返回键。

If you need to iterate over the items in order of their keys, maybe it's better to use a TreeMap instead of a Hashtable. Its keySet method will return the keys in order.

葬シ愛 2024-08-14 03:29:50

将键存储在 ArrayList 中来对它们进行排序怎么样?

    List<String> list = // create from htData.keys()
    Collections.sort(list);
    for(String s : list) {
    ...

What about storing your keys in an ArrayList to sort them?

    List<String> list = // create from htData.keys()
    Collections.sort(list);
    for(String s : list) {
    ...
唐婉 2024-08-14 03:29:50

以下应该有效:

Enumeration<String> enumeration = dictionary.keys(); // unsorted enumeration        
List list= Collections.list(enumeration); // create list from enumeration 
Collections.sort(list);
enumeration = Collections.enumeration(list);

Following should work:

Enumeration<String> enumeration = dictionary.keys(); // unsorted enumeration        
List list= Collections.list(enumeration); // create list from enumeration 
Collections.sort(list);
enumeration = Collections.enumeration(list);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文