1

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!

5
  • 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. Commented Aug 24, 2014 at 0:12
  • In Java code, apart from declarations, belongs in either a constructor or a method. Wrap your put statements in {}, this is called an "instance initialiser" and will be copied to the constructor. Commented Aug 24, 2014 at 0:14
  • Also, it looks like you're passing null values into your map. None of the ArrayLists have been initialized yet. Commented Aug 24, 2014 at 0:14
  • Thanks! I shouldn't have initialized without in a function... Commented Aug 24, 2014 at 0:15
  • 1
    what is the point of having 7 attributes of a class and putting them into a map? Commented Aug 24, 2014 at 0:23

1 Answer 1

3

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.

e.g.,

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.