简体   繁体   English

使用key = String和value = ArrayList创建映射<Double>

[英]Create a map with key=String and value=ArrayList<Double>

I want to create a Map in a Java class, in which key is String and value is with type ArrayList<Double> . 我想在Java类中创建一个Map,其中键是String ,值的类型是ArrayList<Double> My requirement is that, if String equals some value, I could quickly pick up the corresponding list. 我的要求是,如果String等于某个值,我可以快速拿起相应的列表。 All the lists are private variables in the same class. 所有列表都是同一类中的私有变量。 Could someone please tell me where in the following code is wrong? 有人可以告诉我在下面的代码中哪里出错了?

I got compilation error: 我有编译错误:

error: <identifier> expected METRICS_LIST_MAP.put("CPUUtilization", CPUUtilizationList);

My code: 我的代码:

private  ArrayList<Double> CPUUtilizationList;
private  ArrayList<Double> DiskReadOpsList;
private  ArrayList<Double> DiskWriteOpsList;
private  ArrayList<Double> DiskReadBytesList;
private  ArrayList<Double> DiskWriteBytesList;
private  ArrayList<Double> NetworkInList;
private  ArrayList<Double> NetworkOutList;

Map<String, ArrayList<Double>> METRICS_LIST_MAP = new HashMap<String, ArrayList<Double>>();
METRICS_LIST_MAP.put("CPUUtilization", CPUUtilizationList);
METRICS_LIST_MAP.put("DiskReadOps", DiskReadOpsList);
METRICS_LIST_MAP.put("DiskWriteOps", DiskWriteOpsList);
METRICS_LIST_MAP.put("DiskReadBytes", DiskReadBytesList);
METRICS_LIST_MAP.put("DiskWriteBytes", DiskWriteBytesList);
METRICS_LIST_MAP.put("NetworkIn", NetworkInList); 
METRICS_LIST_MAP.put("NetworkOut", NetworkOutList); 

Thanks! 谢谢!

Where is that code located in your class? 你班上的代码在哪里? Is it located outside of all constructors and methods? 它位于所有构造函数和方法之外吗? If so, you can't call the put(...) method out there and instead can only declare or declare and initialize variables and constants. 如果是这样,你不能调用put(...)方法,而只能声明或声明和初始化变量和常量。 Also, always post the complete error message. 此外,始终发布完整的错误消息。 Yours may be missing important statements. 你的可能会遗漏重要的陈述。 So be sure to call the put(...) method calls in a method or constructor block. 所以一定要在方法或构造函数块中调用put(...)方法调用。

You also appear to be passing null values into your map since you don't appear to have initialized any lists yet. 您似乎也将空值传递到地图中,因为您似乎尚未初始化任何列表。 Understand that you don't pass variables into a Map but rather objects and so if the variables have not been assigned a viable object, passing them into the map does nothing. 要知道你没有将变量传递给Map而是传递对象 ,所以如果没有为变量分配一个可行的对象,那么将它们传递到地图中什么都不做。

eg, 例如,

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Foo {
   // initialize all your lists first
   private ArrayList<Double> cPUUtilizationList = new ArrayList<>();
   private ArrayList<Double> diskReadOpsList = new ArrayList<>();
   private ArrayList<Double> diskWriteOpsList = new ArrayList<>();
   private ArrayList<Double> diskReadBytesList = new ArrayList<>();
   private ArrayList<Double> diskWriteBytesList = new ArrayList<>();
   private ArrayList<Double> networkInList = new ArrayList<>();
   private ArrayList<Double> networkOutList = new ArrayList<>();

   private Map<String, ArrayList<Double>> metricsListMap = new HashMap<String, ArrayList<Double>>();

   public Foo() {
      // insert into Map in constructor or method
      metricsListMap.put("CPUUtilization", cPUUtilizationList);
      metricsListMap.put("diskReadOps", diskReadOpsList);
      metricsListMap.put("diskWriteOps", diskWriteOpsList);
      metricsListMap.put("diskReadBytes", diskReadBytesList);
      metricsListMap.put("diskWriteBytes", diskWriteBytesList);
      metricsListMap.put("networkIn", networkInList);
      metricsListMap.put("networkOut", networkOutList);
   }
}

As an aside, you will want to learn and use Java naming conventions . 另外,您将需要学习和使用Java命名约定 Variable names should all begin with a lower letter while class names with an upper case letter. 变量名都应以较低的字母开头,而类名以大写字母开头。

Following these suggestions as well as following good code formatting practices will allow others (such as us!) to better understand your code, and more importantly, will allow your future self to better understand just what you were thinking 6 months ago when you wrote the code. 遵循这些建议以及遵循良好的代码格式化实践将允许其他人(例如我们!)更好地理解您的代码,更重要的是,将允许您的未来自我更好地理解您在6个月前撰写码。

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

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