简体   繁体   中英

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> . My requirement is that, if String equals some value, I could quickly pick up the corresponding list. 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. 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.

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.

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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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