Javascript 对象但适用于 Java

发布于 2025-01-11 19:42:06 字数 1394 浏览 3 评论 0原文

我该如何用 Java 写这个?

//js

const hello = {
  foo: "bar", 
  test: "world", 
  name: "david"
}

我想要一个很长的对象,然后像 hello[test]hello[foo] 一样引用它

我听说过哈希图,但你只能创建一个空的1,然后向其中添加元素。

我在 js 中有一个很长的列表。我怎样才能将它们复制到Java中?一项一项地执行 .put() 会花费很长时间,而且我认为这效率不高。

即使有人编写了一个脚本将 uwu: "owo" 转换为 hello.put("uwu", "owo");,它在带有一大块 hello.put() 的代码。

我也不想为此创建一个新文件(它只有大约 34 行)并希望将其保留在代码中。另外,因为我还有三个类似的文件,每个文件都有 20-40 个键和值,所以我不想创建三个额外的文件,其中只有 30 行。我也不想讨论阅读它们的复杂性。

哦,还有,我不会改变哈希图,只是像常量一样读取数据。

总之,我可以在 Java 中对长列表执行类似的操作而不执行 .put() 吗?

public HashMap<String, String> hello = new HashMap<String, String>(
  "foo": "bar", 
  "test": "world", 
  "name": "david", 
  "uwu": "owo"
);

并像 hello["name"] 一样引用它们?我也不想要这个东西。

public HashMap<String, String> hello = new HashMap<String, String>();
hello.put("foo", "bar");
hello.put("test", "world");
hello.put("name", "david");
hello.put("uwu", "owo");
//for 25 more lines

public HashMap<String, String> hello2 = new HashMap<String, String>();
hello2.put("stuff", "thing");
//... for around 20 more lines

//repeat for 3 more hashmaps

How do I write this in Java?

//js

const hello = {
  foo: "bar", 
  test: "world", 
  name: "david"
}

I want have a very long object, then refer it back like hello[test] or hello[foo]

I've heard of hashmaps, but you can only create an empty one and then add elements into it.

I've got a really long list like that in js. How can I copy those into Java? Doing .put() one by one would take forever, and I don't think that's efficient.

And even if someone wrote a script to turn uwu: "owo" into hello.put("uwu", "owo");, it'd be ugly in the code with a big block of hello.put()s.

I also don't want to create a new file for that (it only has around 34 lines) and want to keep it in the code. Also, because I have three more like these with 20-40 keys and values in each of them, I don't want to create three extra files with just 30 lines in them. I also don't want to go into complexity of reading them.

Oh and also, I won't be changing the hashmap btw, just reading data like a constant.

In summary, can I do something like this in Java for long lists without doing .put()?

public HashMap<String, String> hello = new HashMap<String, String>(
  "foo": "bar", 
  "test": "world", 
  "name": "david", 
  "uwu": "owo"
);

And refer to them like hello["name"]? I also don't want this thing.

public HashMap<String, String> hello = new HashMap<String, String>();
hello.put("foo", "bar");
hello.put("test", "world");
hello.put("name", "david");
hello.put("uwu", "owo");
//for 25 more lines

public HashMap<String, String> hello2 = new HashMap<String, String>();
hello2.put("stuff", "thing");
//... for around 20 more lines

//repeat for 3 more hashmaps

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以畏孤独 2025-01-18 19:42:07

在 Java 14 及更高版本中,我建议使用记录,如其他答案中所述。
这是最安全的,也可能是最有效的方法。

对于 Java 9 到 14,您可以使用 Map.of("hello", "world", "foo", "bar");
但您可能无法超出一定数量的键/值对。

对于 java 8 及更低版本,或者如果超过 Map.of 允许的参数数量,您没有其他选择,只能创建一个空映射并一一放置键/值对。
但请注意,性能并不一定会变差。
您当然可以使用可变数量的参数重新实现您自己的 Map.of 版本。

IN Java 14 and beyond, I would recommand using a record, as explained in the other answer.
It's the safest and also probably the most efficient way.

For Java 9 to 14, you may use Map.of("hello", "world", "foo", "bar");.
But you may not be able to go beyond a certain number of key/value pairs.

For java 8 and below, or if you exceed the number of arguments allowed with Map.of, you don't have other choice than create an empty map and put key/value pairs one by one.
Note however that, performances aren't necessarily going to be worse.
You can of course reimplement your own version of Map.of with variable number of arguments.

爱格式化 2025-01-18 19:42:07

由于您需要一些常量,因此您可以将这些值保存在文件中并从这些文件中读取。例如以 json 格式将数据保存在文件中:

{
  "foo": "bar", 
  "test": "world", 
  "name": "david"
}

然后将此文件解析为 Map

public class Main {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = mapper.readValue(ClassLoader.getSystemResourceAsStream("constants.json"), Map.class);
        map.forEach((k, v) -> System.out.println(k + " -> " + v));
    }
}

此示例使用读取文件作为项目资源,并使用 ObjectMapper 将 json 解析为 Map,但您可以使用任何其他工具来达到相同的效果。如果数据格式足够简单(字符串键到字符串值,没有嵌套数组、对象等),您可以将其保存为更简单的格式,并手动执行读取、解析、添加到映射。

Since you need something constant like, you can save those values in files and read from those files. For example save data in file in json format:

{
  "foo": "bar", 
  "test": "world", 
  "name": "david"
}

Then parse this file to a Map.

public class Main {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> map = mapper.readValue(ClassLoader.getSystemResourceAsStream("constants.json"), Map.class);
        map.forEach((k, v) -> System.out.println(k + " -> " + v));
    }
}

This example uses reading file as project resource and uses ObjectMapper to parse json to Map, but you can use any other tool for the same effect. If the data format is simple enough(string key to string value, no nested arrays, objects and such) you can save it in even simpler format and do the read, parse, add to map manually.

苏大泽ㄣ 2025-01-18 19:42:06

在现代 Java(14 及更高版本)中,您可以使用 record:

    record Hello(String foo, String test, String world) { }

并创建一个如下所示的实例:

    final Hello hello = new Hello("bar", "world", "david");

您可以访问如下值:

System.out.print(hello.foo());

使用记录的优点是您的数据是静态类型的 - 您可以不要输错密钥,或者忘记删除已从记录中删除的密钥的使用情况。

In modern Java (14 and later) you can use a record:

    record Hello(String foo, String test, String world) { }

and create an instance like this:

    final Hello hello = new Hello("bar", "world", "david");

You access the values like:

System.out.print(hello.foo());

Using a record has the advantage that your data is statically typed -- you can't mistype a key, or forget to remove usages of a key you've removed from the record.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文