在 Java 中是否有编写地图文字样式的最佳实践?
简而言之,如果您想在 Java 中编写常量的映射(例如在 Python 和 Javascript 中您将编写为文字),
T<String,String> CONSTANTS =
{
"CONSTANT_NAME_0": CONSTANT_VALUE_0 ,
"CONSTANT_NAME_1": CONSTANT_VALUE_1 ,
"CONSTANT_NAME_2": CONSTANT_VALUE_2 ,
//...
} ;
是否有一个 Class
或任何预设的 Object
你可以用它来编写这样的数据结构吗?
In short, if you want to write a map of e.g. constants in Java, which in e.g. Python and Javascript you would write as a literal,
T<String,String> CONSTANTS =
{
"CONSTANT_NAME_0": CONSTANT_VALUE_0 ,
"CONSTANT_NAME_1": CONSTANT_VALUE_1 ,
"CONSTANT_NAME_2": CONSTANT_VALUE_2 ,
//...
} ;
is there a Class
or any preset Object
that you can use for writing a data structure like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我喜欢这样做:
双 {{ }} 是实例初始化块。它们有点不寻常,但很有用。不需要图书馆或助手。
I like to do it this way:
The double {{ }} are an instance initialization block. They are a bit unusual but they are useful. No need for libraries or helpers.
不,Java 没有地图文字。最接近的方法是使用 Google Collections 的 不可变映射:
No, Java doesn't have a map literal. The closest you'll come to this is using Google Collections' ImmutableMap:
常数?我会使用 enum。
例如
NAME_2
的值可以通过如下方式获得:只给枚举一个更合理的名称,例如
Settings
、Defaults
等,无论那些名称/值对实际上表示。Constants? I'd use an enum.
Value for e.g.
NAME_2
can be obtained as follows:Only give the enum a bit more sensible name, e.g.
Settings
,Defaults
, etc, whatever those name/value pairs actually represent.抱歉,我是一个修补匠:-) 这是一种更干净的方法。
Sorry, I'm a tinkerer :-) Here's a somewhat cleaner way.
您可以自己编写一个快速帮助函数:
请注意,上面的代码不会阻止您覆盖同名常量,在列表中的多个位置使用CONST_1 的值出现。
CONST_1
将得到最终的用法是这样的:
You can write yourself a quick helper function:
Note the code above isn't going to stop you from overwriting constants of the same name, using
CONST_1
multiple places in your list will result in the finalCONST_1
's value appearing.Usage is something like:
从 Java 9 开始,您可以开箱即用地创建不可修改的映射 -
有支持最多 10 个密钥对的变体。
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html#of()
这也受支持 https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html#ofEntries(java.util.Map.Entry.. .)
Since Java 9, you can create unmodifiable maps out of the box -
there are variants supporting upto 10 keypairs.
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html#of()
This is also supported https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Map.html#ofEntries(java.util.Map.Entry...)
这是另一种方法,最适合不会改变的地图:
Here's another way, best suited for maps that won't be changing:
Java7 假设实现以下语法:
但是现在您被迫使用 Jorn 或 Tony Ennis 提出的解决方案。
Java7 suppose to implement following syntax:
However now you're forced to use solutions proposed by Jorn or Tony Ennis.
好吧,随着乔恩的进步,我似乎根本无法改变地图,无论是内部还是外部。也许不太可读,但如果您需要地图不可修改,我认为这更好。
Ok, with Jorn's improvement I can't seem to change the map at all, internally or externally. Perhaps not quite as readable, but if you need the map to be unmodifiable I think this is better.
我喜欢在同一行进行声明和初始化。我已经使用这个方便的小实用程序这么长时间了,它基本上是我的“地图文字”,直到它们在语言中“正确”完成之前,我将继续像这样使用它: )
很高兴在这里分享。
I like to do the declaration and initialization on the same line. I've used this handy little utility for so long it basically is my "map literal" and until they're done "right" in the languange, I'm gonna continue on using it like that :)
Happy to share it here.