如何在Java中按字母顺序排序枚举成员?

14

我有一个枚举类,如下所示:

public enum Letter {
    OMEGA_LETTER("Omega"), 
    GAMMA_LETTER("Gamma"), 
    BETA_LETTER("Beta"), 
    ALPHA_LETTER("Alpha"), 

    private final String description;

    Letter() {
      description = toString();
    }

    Letter(String description) {
      this.description = description;
    }

    public String getDescription() {
      return description;
    }
}

在我的代码稍后部分,我基本上遍历字母枚举并将其成员打印到控制台:

for (Letter letter : Letter.values()) {
System.out.println(letter.getDescription());
}

我原以为使用values()方法能够按照枚举常量的顺序获取该枚举的有序视图(正如这里所述),但是实际情况并非如此。事实上,我只是按照在Letter枚举类中创建它们的顺序获取枚举成员。是否有一种方法可以按字母顺序输出枚举值?我需要一个单独的比较器对象吗,还是有内置的方法可以实现?基本上,我希望根据getDescription()文本对值进行字母排序:

Alpha
Beta
Gamma
Omega

1
你链接的文章没有错。他所指的排序是它们在枚举中被输入的顺序。编译器/运行时不会对枚举值进行任何特殊的排序。 - Suppressingfire
我在stackoverflow上找到了这个链接,对我很有用。http://stackoverflow.com/a/18416259/6301287 - Bruno Gerotto
5个回答

23
SortedMap<String, Letter> map = new TreeMap<String, Letter>();
for (Letter l : Letter.values()) {
    map.put(l.getDescription, l);
}
return map.values();

或者只需重新排列声明即可 :-)

编辑:正如KLE所指出的那样,这假设描述在枚举内是唯一的。


6
为什么要使用Map?因为键没有被使用,所以它并不能很好地传达意图,对吗?使用SortedSet(实现为TreeSet)可能更合适。但是,在原始帖子中并没有提到Set(或Map的键)所特有的唯一性作为要求,因此我建议使用更简单(且更高效)的List<String>,并在其后调用Collections.sort() - KLE
1
键按顺序排序,值被返回。问题陈述如下:“基本上我希望根据getDescription()文本对值进行字母顺序排序:”,因此也许仅打印枚举并不是他的全部想法。 - meriton
@meriton 感谢您的赞赏。正如我在我的答案中所评论的,原始帖子中的“value”一词没有被定义,但我理解它是指“description”字段的值。在您的代码示例和评论中,您假设“value”表示instance。(当然,在我的代码中,我会同意您的观点; 但对于joel_nc来说呢?) - KLE

6
我以为values()方法会按照枚举的顺序给我一个有序视图(就像这里提到的那样),但是这并不是这里的情况。我只是按照Letter枚举类中创建它们的顺序获得枚举成员。
确切地说,对于枚举来说,声明的顺序被认为是重要的,因此我们很高兴它们恰好按照那个顺序返回。例如,当一个int i表示枚举值时,使用values()[i]是一个非常简单和有效的方法来找到枚举实例。相反,ordinal()方法返回枚举实例的索引。
有没有一种方法可以按字母顺序输出枚举的值?我需要一个单独的比较器对象吗,还是有内置的方法可以做到这一点?基本上,我希望根据getDescription()文本对值进行字母排序:
在一般情况下,你所谓的value并不是通用的枚举定义。在这里,你的上下文中,你指的是getDescription()的结果。
正如你所说,你可以为这些描述创建一个比较器。那将是完美的 :-)
请注意,在一般情况下,你可能需要这些实例的多个顺序:
  • 声明顺序(这是官方顺序)
  • 描述顺序
  • 其他需要的顺序
你也可以推动DescriptionComparator的概念:
  1. 出于性能原因,你可以存储计算出的描述。

  2. 因为枚举不能继承,代码重用必须在枚举类之外。让我给出我们在项目中使用的例子:

现在是代码示例...
/** Interface for enums that have a description. */
public interface Described {
  /** Returns the description. */
  String getDescription();
}

public enum Letter implements Described {
  // .... implementation as in the original post, 
  // as the method is already implemented
}

public enum Other implements Described {
  // .... same
}

/** Utilities for enums. */
public abstract class EnumUtils {

  /** Reusable Comparator instance for Described objects. */
  public static Comparator<Described> DESCRIPTION_COMPARATOR = 
    new Comparator<Described>() {
      public int compareTo(Described a, Described b) {
        return a.getDescription().compareTo(b.getDescription);
      }
    };

  /** Return the sorted descriptions for the enum. */
  public static <E extends Enum & Described> List<String> 
    getSortedDescriptions(Class<E> enumClass) {
      List<String> descriptions = new ArrayList<String>();
      for(E e : enumClass.getEnumConstants()) {
        result.add(e.getDescription());
      }
      Collections.sort(descriptions);
      return descriptions;
  }
}

// caller code
List<String> letters = EnumUtils.getSortedDescriptions(Letter.class);
List<String> others = EnumUtils.getSortedDescriptions(Other.class);

请注意,EnumUtils 中的通用代码不仅适用于一个枚举类,而且还可用于您项目中实现 Described 接口的任何枚举类。
正如之前所说,将代码放在枚举外部(本应该放在枚举内部的位置)是为了重复利用代码。对于两个枚举来说并不是大问题,但我们的项目中有超过一千个枚举,其中许多都使用相同的接口...!

5
只需使用Arrays.sort和自己的比较器即可对它们进行排序。

3
你可以在Java8中使用比较器与sorted函数。
enumlist.stream()
.sorted(Comparator.comparing(Enum::toString)).collect(Collectors.toList());

0

这里有一种通用的方法,可以在不实现Comparable接口或创建自定义比较器的情况下对任何类进行排序。我发现有些情况下我不想覆盖compareTo方法,因为它有不同的用途,而且对于枚举类型来说是不行的,而且不断创建包装类也很麻烦。你可以传入一个函数,该函数输出一个Comparable对象,你可以用它来进行排序。

toComparable函数每个元素只调用一次(自定义比较器不是这样),所以如果某个类的调用很昂贵,它尤其好用。空值在内部处理,因此使用起来比自定义比较器更容易。一次调用Java 7的TimSort算法比对SortedMap(红黑树或其他平衡树实现)进行多个O(log N)插入要高效得多。而且你不受任何特定类或接口的限制。

在许多情况下,真实世界的性能提升是显著的。例如,当使用toString()对大小为100k的列表中的Double进行排序时,与使用比较器相比,性能提高了约5倍。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class GenericLetterSorter {
    public enum Letter {
        OMEGA_LETTER("Omega"), 
        GAMMA_LETTER("Gamma"), 
        BETA_LETTER("Beta"), 
        ALPHA_LETTER("Alpha"); 

        private final String description;

        Letter() {
          description = toString();
        }

        Letter(String description) {
          this.description = description;
        }

        public String getDescription() {
          return description;
        }
    }

public static void main(String[] args) {
    List<Letter> list = new ArrayList<>(Arrays.asList(Letter.values()));

    sort(list, new ToComparable<Letter>() {
        @Override
        public Comparable toComparable(Letter letter) {
            // sort based on the letter's description
            return letter == null ? null : letter.getDescription();
        }
    });

    for (Letter letter : list)
        System.out.println(letter == null ? null : letter.name());
}

    public interface ToComparable<T, C extends Comparable<? super C>> {
         C toComparable(T t);
    }

    public static <T, C extends Comparable<? super C>> void sort(List<T> list, ToComparable<T, C> function) {
       class Pair implements Comparable<Pair> {
          final T original;
          final C comparable;

          Pair(T original, C comparable) {
             this.original = original;
             this.comparable = comparable;
          }

          @Override
          public int compareTo(Pair other) {
                return
                  comparable == null && other.comparable == null ? 0 :
                  comparable == null ? -1 :
                  other.comparable == null ? 1 :
                  comparable.compareTo(other.comparable);
          }
       }

       List<Pair> pairs = new ArrayList<>(list.size());
       for (T original : list)
          pairs.add(new Pair(original, function.toComparable(original)));

       Collections.sort(pairs);

       ListIterator<T> iter = list.listIterator();
       for (Pair pair : pairs) {
          iter.next();
          iter.set(pair.original);
       }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接