有没有一种简单的方法可以在Java中按字母顺序对字符串枚举进行字母排序? [英] Is there a simple way to alphabetize a String Enumeration in Java?

查看:155
本文介绍了有没有一种简单的方法可以在Java中按字母顺序对字符串枚举进行字母排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说.

我尝试用List []上的Collections.sort()和ArrayList的.sort()函数弄乱了一点,但是我却无法将其解析回Enumeration.

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.

谢谢!

这里有一些伪码和进一步的解释.我的目标是从Hashtable中获取keys()并按字母顺序进行涉及每个键的复杂操作.

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.

我当前的流程是:

  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
         */
    }
}

问题在于,枚举中的数据必须按字母顺序排列(即,必须按字母顺序对每个键执行复杂过程").有什么办法吗?谢谢!

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!

推荐答案

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

If you have a sorted list you can use

Collections.enumeration(myList)

Collections.enumeration(myList)

将其恢复为枚举...如果我正确关注您的话.

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

您可以执行此操作...

You can do this...

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

这篇关于有没有一种简单的方法可以在Java中按字母顺序对字符串枚举进行字母排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆