Storing EnumSet in a database?
问题 So in C++/C# you can create flags enums to hold multiple values, and storing a single meaningful integer in the database is, of course, trivial. In Java you have EnumSets, which appear to be quite a nice way to pass enums around in memory, but how do you output the combined EnumSet to an integer for storage? Is there another way to approach this? 回答1: // From Adamski's answer public static <E extends Enum<E>> int encode(EnumSet<E> set) { int ret = 0; for (E val : set) { ret |= 1 << val