本文介绍了只需一步即可在HashMap中声明和放置String数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将静态数据插入到Java中的 HashMap 中,如下所示:
I am trying to insert static data into a HashMap in Java like this:
HashMap<String,String[]> instruments = new HashMap<String, String[]>();
instruments.put("EURUSD", {"4001","EURUSD","10000","0.00001","0.1","USD"});
但编译器不喜欢它。我发现将数据插入HashMap的唯一方法是分别声明字符串数组,然后将其放入HashMap,就像这样
But the compiler doesn't like it. The only way I found to insert that data into the HashMap is to declare the string array separately and then put it into the HashMap, like this
String[] instruDetails = {"4001","EURUSD","10000","0.00001","0.1","USD"};
instruments.put("EURUSD", instruDetails);
但它不具有表现力,难以维持
But it not very expressive, and hard to maintain
所以我的问题是,有没有办法在一步/一行中执行 put()
操作和字符串数组声明?
So my question is, is there a way to do the put()
operation and string array declaration in one step/line?
推荐答案
这样做:
instruments.put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});
这篇关于只需一步即可在HashMap中声明和放置String数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!