Counting standard deviation from parsed XML file I wrote a method that calculates the average and standard deviation of sell currency exchange-course (ASK).  But I think that it isn't the easiest way to define such behavior.


In my opinion there is too much creating of BigDecimal values.
I have also doubts about using an ArrayList to get values in order to count standard deviation, maybe there is a better data structure to achieve that goal.


The method contains also counting average of buy currency exchange-course (BID in parsed file) but don't focus on it please.

package pl.parser.nbp;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Counting {
public void countAverageAndStandartDeviaton(String address){
    try
    {
        DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = df.newDocumentBuilder();
        Document doc = db.parse(address);
        doc.getDocumentElement().normalize();

        NodeList nList = doc.getElementsByTagName("Rate");

        List&lt;BigDecimal&gt; listForStandartDeviation = new ArrayList&lt;&gt;();
        BigDecimal averageOfBid = new BigDecimal("0");
        BigDecimal averageOfAsk = new BigDecimal("0");
        BigDecimal divisor = new BigDecimal(nList.getLength());

        for (int temp = 0; temp &lt; nList.getLength(); temp++) {

            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                BigDecimal valueForBidAverage = new BigDecimal(eElement.getElementsByTagName("Bid").item(0).getTextContent());
                BigDecimal valueForStandartDeviation = new BigDecimal(eElement.getElementsByTagName("Ask").item(0).getTextContent());
                averageOfBid = averageOfBid.add(valueForBidAverage);
                averageOfAsk = averageOfAsk.add(valueForStandartDeviation);
                listForStandartDeviation.add(valueForStandartDeviation);


            }
        }
        averageOfBid = new BigDecimal(averageOfBid.divide(divisor).toString()).setScale(4, RoundingMode.HALF_UP);
        averageOfAsk = new BigDecimal(averageOfAsk.divide(divisor).toString());
        System.out.println(averageOfBid + " - BID Average");

        BigDecimal sumStandartDeviation = new BigDecimal("0");
        for(int i = 0 ; i&lt;listForStandartDeviation.size(); i++){
            BigDecimal valueFromList = new BigDecimal(listForStandartDeviation.get(i).toString());
            sumStandartDeviation = sumStandartDeviation.add((valueFromList.subtract(averageOfAsk)).pow(2));
        }
        sumStandartDeviation = sumStandartDeviation.divide(divisor);
        sumStandartDeviation = new BigDecimal(Math.sqrt(sumStandartDeviation.doubleValue()))
                .setScale(4, RoundingMode.HALF_UP);
        System.out.println(sumStandartDeviation +  " - ASK Standart Deviation");

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
}
Be careful with instation of same object multiple times for same use- as it may change in use. If and when the first instantiation will be disposed, however, depends. The Java runtime environment traces references, so as long as there are any references to an object in use, the object won't be disposed. Note that the dispose itself does not happen immediately, but sometimes later, the next time when the garbage collector is run. But that is independent from the semantics of the code discussed, thats only a question of memory and performance.
I would use a HashMap for the Data Structure to count deviation. 
Such as this example: 

  HashMap hm = new HashMap();
  // Put elements to the map
  hm.put("Zara", new Double(3434.34));
  hm.put("Mahnaz", new Double(123.22));
  hm.put("Ayan", new Double(1378.00));
  hm.put("Daisy", new Double(99.22));
  hm.put("Qadir", new Double(-19.08));