Question

I am getting an error when I try to retrieve the individual values to find the variance in the process of calculating the standard deviation. I can't figure out whether to use .get() or .getValue and I am lost. I have already calculated the average.

final ArrayList<Map.Entry<String,NumberHolder>> entries = new ArrayList<Map.Entry<String,NumberHolder>>(uaCount.entrySet());


for(Map.Entry<String,NumberHolder> entry : entries)  //iterating over the sorted hashmap
{

    double temp = 0;
    double variance = 0;

    for (int i = 0; i <= entry.getValue().occurrences ; i ++)
        {               
            temp += ((entry.getValue(i).singleValues) - average)*((entry.getValue(i).singleValues) - average);

            variance = temp/entry.getValue().occurrences;
        }

        double stdDev = Math.sqrt(variance);

This is my NumberHolder class, which I populate in my main funcion. I am using this equation for standard deviation: http://www.mathsisfun.com/data/standard-deviation-formulas.html

based on my code, occurrences is N and the values from the singleValues arraylist are Xi

public static class NumberHolder
{
    public int occurrences = 0;
    public int sumtime_in_milliseconds = 0; 
    public ArrayList<Long> singleValues = new ArrayList<Long>();
}

This is the error I get. :

The method getValue() in the type Map.Entry<String,series3.NumberHolder> is not applicable for the arguments (int). 

If you need to see more code please just ask, I didn't want to put anything unnecessary but I might have missed something.

Was it helpful?

Solution 2

You can't take int as argument in Map.Entry#getValue(). So in your code it should be entry.getValue() instead of entry.getValue(i). Now apart from this your singleValues is an ArrayList . so you can't subtract it from integer average in line (entry.getValue(i).singleValues) - average). You have to first extract out the element from the ArrayList and then subtract it from the average. Your for loop should be something like this:

for (int i = 0; i < entry.getValue().occurrences ; i ++)// i < entry.getValue() to avoid IndexOutOfBoundsException
{               
   temp += ((entry.getValue().singleValues.get(i)) - average)*((entry.getValue().singleValues.get(i)) - average);
   variance = temp/entry.getValue().occurrences;
}

OTHER TIPS

Error means exactly what it says. You can't pass an int argument to getValue().

Change entry.getValue(i) to entry.getValue() and it should work fine.

I assume it is something like entry.getValue().singleValues.get(i) that you want. If occurrences is always equal to entry.getValue().singleValues.size() consider getting rid of it.

getValue doesn't take an integer argument. You can use:

for (int i = 0; i < entry.getValue().singleValues.size(); i++) {
   Long singleValue = entry.getValue().singleValues.get(i);
   temp += (singleValue - average) * (singleValue - average);

   variance = temp / entry.getValue().occurrences;
}

Also ArrayLists are zero based so you should end on size - 1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top