简体   繁体   English

如何存储 Java 常量以用于构建键值组合

[英]How to store Java constants for use in building key value combinations

Let's say I want to store potential keys and potential values for those keys as constants.假设我想将这些键的潜在键和潜在值存储为常量。 How can I achieve this?我怎样才能做到这一点? Or should I avoid it altogether?还是我应该完全避免它?

This is the aproach that I thought of myself, but as you'll be able to see, it has an obvious downfall.这是我自己想到的方法,但正如你所看到的,它有一个明显的失败。

public static class Foo { 

    public static final String KEY = "foo" ; 

    public static class Values { 

        public static final String BAR = "bar" ; 
        public static final String HEY = "hey" ; 

    }

}

public static class Another { 

    public static final String KEY = "another" ; 

    public static class Values { 

        public static final String ONE = "1" ; 
        public static final String TWO = "two" ;
        public static final String THREE = "THREE" ;

    }

}

Which allows me to access these keys like so这使我可以像这样访问这些密钥

miscellaneousMethod( Foo.KEY, Foo.Values.BAR )
miscellaneousMethod( Another.KEY, Another.Values.TWO )

However, I don't exactly want to write a separate static inner class for each key / possible-values pair.但是,我并不完全想为每个键/可能值对编写一个单独的 static 内部 class 。

Is there a better way to store key value pairs as constants?有没有更好的方法将键值对存储为常量?

I want to store them as constants for later comparison with generated hashmaps.我想将它们存储为常量,以便以后与生成的哈希图进行比较。 So that I can ask stuff like this:这样我就可以问这样的东西:

if( map.get( Foo.KEY ).equals( Foo.Values.HEY ) ) { /* do stuff */ }

If they are all constants, you might use an Enum:如果它们都是常量,则可以使用 Enum:

public enum ValueEnum {

    FOO("foo", "bar", "hey"),
    ANOTHER("another", "1", "two", "THREE"),

    ;

    private final String key;

    private final Set<String> values;

    private ValueEnum(String key, String... values) {
        this.key = key;
        this.values = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(values)));
    }

    public final boolean isInMap(Map<String,String> map) {
        if(map.containsKey(key)) {
            return values.contains(map.get(key));
        }
        else {
            return false;
        }
    }
}

then然后

if( ValueEnum.FOO.isInMap(map) ) { /* do stuff */ }

Please avoid constants like that.请避免使用这样的常量。 For constants use the Java enum type.对于常量,使用 Java 枚举类型。 It compiles under the hood as classes so you get type safety, and you can also use them in switch statements.它在后台编译为类,因此您可以获得类型安全,并且您还可以在 switch 语句中使用它们。 It's nice to be able to add methods to them too.也很高兴能够向它们添加方法。

There's a nice example here: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html这里有一个很好的例子: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

A longer discussion (with many examples) is here: http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html更长的讨论(有很多例子)在这里: http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.ZFC35FDC70D5FC69D269883A822C75A

If possible, can you not just specify these keys in an XML file and use Java XML binding (JAXB) to load them.如果可能的话,您能否仅在 XML 文件中指定这些键并使用 Java XML 绑定(JAXB)来加载它们。

  • you would avoid having to rebuild and deploy your code when and if the keys change.您将避免在密钥更改时重新构建和部署代码。

Put the constants in a Map and then make it unmodifiable using the method Collections.unmodifiableMap() .将常量放入Map中,然后使用Collections.unmodifiableMap()方法使其不可修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM