title: Group map elements and sum their values in Java
body: <p>I read the below data from a CSV file. </p>
  
  <pre><code>Pedro|groceries|apple|1.42 
  Nitin|tobacco|cigarettes|15.00 
  Susie|groceries|cereal|5.50 
  Susie|groceries|milk|4.75 
  Susie|tobacco|cigarettes|15.00 
  Susie|fuel|gasoline|44.90 
  Pedro|fuel|propane|9.60 
  </code></pre>
  
  <p>I wanted to group the expenses of each customer like </p>
  
  <p>Expense of Pedro:</p>
  
  <p>Groceries - 1.42
  fuel - 9.62</p>
  
  <p>I also wanted to group the sum of the expense for each customer like</p>
  
  <p>Total expense for each customer is:</p>
  
  <p>Pedro - (1.42 + 9.60)
  Nitin - 15.00
  Susie - (5.50 + 4.75 + 15.00 + 44.90)</p>
  
  <p>Can someone help me in how will I group the elements and sum their values. </p>
  
  <p>I am able to read the file and print separate values. I mapped each group member with their expenses. But don't know how to sum up their values</p>
  
  <p>can Someone help me?</p>
  
  <p>This is my code, im sure this is incorrect</p>
  
  <pre><code>static String[] inputArray ;
  
  
  public static void main(String[] args) throws IOException {
  
      groceryReport gr = new groceryReport();
  
      try {
          gr.readFile();
          System.out.println(gr.customerPurchase(inputArray).toString());
          }
      catch (FileNotFoundException e) {
  
          e.printStackTrace();
      }
  }
  
  @SuppressWarnings("null")
  private void readFile() throws IOException {
  
      BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\....\\grocery.csv")); 
      String input= "";
      try{
          System.out.println("The total revenue for each customer is:");
  
      while((input = br.readLine()) != null )
      {
      inputArray= input.split("\\|");
      }
      }
      catch(Exception e){
          e.printStackTrace();
      }
      finally {
          if (br != null) {
              try {
                  br.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  
  
  
      public String customerPurchase(String[] inputArray){
  
      String sum = "" ; float sum1=0; float sum2 = 0;
      ArrayList&lt;String[]&gt; alist = new ArrayList&lt;String[]&gt;();
      alist.add(inputArray);
      String value = "";
      System.out.println(alist);
      Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
      map.put(inputArray[0], inputArray[3]);
      System.out.println(map.get(inputArray));
      Iterator ite = map.entrySet().iterator();
  
      while (ite.equals("Susie"))
          ite.next();
          value = map.get("Susie");
  
              sum = sum+ value;
  
  
              return sum;
  
  
  }
  </code></pre>