我试图将静态数据插入Java中的 HashMap 中,如下所示:

HashMap<String,String[]> instruments = new HashMap<String, String[]>();
instruments.put("EURUSD", {"4001","EURUSD","10000","0.00001","0.1","USD"});

但是编译器不喜欢它。我发现将数据插入到HashMap中的唯一方法是分别声明字符串数组,然后将其放入HashMap中,如下所示
String[] instruDetails = {"4001","EURUSD","10000","0.00001","0.1","USD"};
instruments.put("EURUSD", instruDetails);

但是它不是很富有表现力,并且很难维护

所以我的问题是,有没有一种方法可以一步一步地完成put()操作和字符串数组声明?

最佳答案

可以做到这一点:

instruments.put("EURUSD", new String[]{"4001","EURUSD","10000","0.00001","0.1","USD"});

09-27 13:43