Search code examples
javaarraystreemap

Sum arrays from TreeMap


I have multiple arrays stored in a TreeMap, then I want to retrieve these arrays and multiply them by a number. The after that I want to sum each column values in a new array.

import java.util.Arrays;
import java.util.Map;
import java.util.TreeMap;

public class readingLargeFile {
    static String[] words;
    static  Map<String, double[]> m1 = new TreeMap();
    static Map<String,Double> m2 = new TreeMap();
    public static void main(String args[]){
        //First TreeMap
         double count[]={3.9, 1.2, 6.2, 1.8, 7.6, 3.8};
         double count1[]={1.6, 7.2, 6.2, 2.3, 1.8, 0.0};
         double count2[]={1.6, 5.5, 1.8, 8.8, 0.0, 0.0};
         double count3[]={0.0, 0.0, 0.0, 2.3, 0.0, 0.0};
         double count4[]={2.0, 2.2, 1.2, 3.9, 2.3, 4.4};
         double count5[]={3.4, 0.0, 1.9, 2.4, 0.5, 2.0};
            m1.put("apple",count);
            m1.put("orange",count1);
            m1.put("banana",count2);
            m1.put("cherry",count3);
            m1.put("lemon",count4);
            m1.put("strawbarry",count5);

        //for(Map.Entry<String, double []> e : m1.entrySet()) {//print First TreeMap content

        //System.out.println(e.getKey()+":"+Arrays.toString(e.getValue()));
            //}

               //second TreeMap
        m2.put("apple", 2.1);
        m2.put("cherry", 1.9);
        m2.put("grasb", 3.0);
        m2.put("strawbarry", 4.1);

        double[] finalSum = new double[6];
        double first=0;
        double second=0;
        double third=0;
        double fourth=0;
        double fifth=0;
        double sixth=0;

        String key="";
                 for ( Map.Entry<String,Double> entry : m2.entrySet() ) {//for loop through the second TreeMap
                      if ( m1.containsKey(entry.getKey()) ) {//check if the first TreeMapp contain same key from second TreeMap
                          //if the key is common in m1 and m2, multiply the values

                          key=entry.getKey();
                       double y=entry.getValue();//the number from second TreeMap 
                       double w[]=m1.get(entry.getKey());//the array from first TreeMap
                       //for loop to multiple the array from first TreeMap by the number in second TreeMap for same key
                       /*for example if the key=apple
                         then
                         {3.9*2.1, 1.2*2.1, 6.2*2.1, 1.8*2.1, 7.6*2.1, 3.8*2.1}
                         */ 
                       for (int u=0;u<w.length;u++){
                           finalSum[u]=  w[u]*y;}
                       System.out.println(key+"     "+Arrays.toString(finalSum));
                       }}


                 for (int u=0,t=0;u<finalSum.length;u++,t++){
                   first+=finalSum[t];
                   second+=finalSum[t];
                   third+=finalSum[t];
                   fourth+=finalSum[t];
                   fifth+=finalSum[t];
                   sixth+=finalSum[t];
                 }
                   System.out.println(first+"\n"+second+"\n"+third+"\n"+fourth+"\n"+fifth+"\n"+sixth);}}

Then this my output:

apple     [8.19, 2.52, 13.020000000000001, 3.7800000000000002, 15.959999999999999, 7.9799999999999995]
cherry     [0.0, 0.0, 0.0, 4.369999999999999, 0.0, 0.0]
strawbarry     [13.939999999999998, 0.0, 7.789999999999999, 9.839999999999998, 2.05, 8.2]
41.81999999999999
41.81999999999999
41.81999999999999
41.81999999999999
41.81999999999999
41.81999999999999

Anyone know what's the problem.:(


Solution

  • FINAL EDIT After the reconstructed question, i have made changes to formerly proposed solutions, though still using the same aproach, see below at "original answer" for details

    Here is the final fixed code for the issue, adressing the issues as formerly stated.

    import java.util.Arrays;
    import java.util.Map;
    import java.util.TreeMap;
    
    public class ReadingLargeFile {
        static String[] words;
        static Map<String, double[]> m1 = new TreeMap();
        static Map<String, Double> m2 = new TreeMap();
    
        public static void main(String args[]) {
            //First TreeMap
            double count[] = {3.9, 1.2, 6.2, 1.8, 7.6, 3.8};
            double count1[] = {1.6, 7.2, 6.2, 2.3, 1.8, 0.0};
            double count2[] = {1.6, 5.5, 1.8, 8.8, 0.0, 0.0};
            double count3[] = {0.0, 0.0, 0.0, 2.3, 0.0, 0.0};
            double count4[] = {2.0, 2.2, 1.2, 3.9, 2.3, 4.4};
            double count5[] = {3.4, 0.0, 1.9, 2.4, 0.5, 2.0};
            m1.put("apple", count);
            m1.put("orange", count1);
            m1.put("banana", count2);
            m1.put("cherry", count3);
            m1.put("lemon", count4);
            m1.put("strawbarry", count5);
    
            //second TreeMap
            m2.put("apple", 2.1);
            m2.put("cherry", 1.9);
            m2.put("grasb", 3.0);
            m2.put("strawbarry", 4.1);
    
            double[][] addedresults = getFinalSums(m1, m2);
            double[] finalResults = getSummedColumns(addedresults);
    
            System.out.println("added value arrays: " + Arrays.deepToString(addedresults));
            System.out.print("final summed array: " + Arrays.toString(finalResults));
    
        }
    
        public static double[] getSummedColumns(double[][] array) {
            double[] results = new double[array.length];
            for (int i = 0; i < results.length; i++) {
                for (int j = 0; j < array.length; j++) {
                    results[i] += array[j][i];
                }
            }
            return results;
        }
    
        public static double[][] getFinalSums(Map<String, double[]> m1, Map<String, Double> m2) {
    
            int sharedSums = 0;
            for (Map.Entry<String, Double> entry : m2.entrySet())
                if (m1.containsKey(entry.getKey()))
                    sharedSums++;
    
            double[][] finalSum = new double[sharedSums][];
    
            int i = 0;
            for (Map.Entry<String, Double> entry : m2.entrySet()) {//for loop through the second TreeMap
                if (m1.containsKey(entry.getKey())) {//check if the first TreeMapp contain same key from second TreeMap
                    //if the key is common in m1 and m2, multiply the values
    
                    double y = entry.getValue();//the number from second TreeMap
                    double w[] = m1.get(entry.getKey());//the array from first TreeMap
    
                    finalSum[i] = new double[w.length]; // instantiate the new array in the 2d array
    
                    for (int j = 0; j < w.length; j++) {
                        finalSum[i][j] = w[j] * y; // add in the values in the correct spot in the 2d array
                    }
                    i++; // increase the current spot in the 2d array
                }
            }
            return finalSum;
        }
    }
    

    Here is the output produced by this class

    added value arrays: [[8.19, 2.52, 13.020000000000001, 3.7800000000000002, 15.959999999999999, 7.9799999999999995], [0.0, 0.0, 0.0, 4.369999999999999, 0.0, 0.0], [13.939999999999998, 0.0, 7.789999999999999, 9.839999999999998, 2.05, 8.2]]

    final summed array: [22.129999999999995, 2.52, 20.810000000000002]

    EDIT Original Answer

    for (int u=0;u<w.length;u++){
        finalSum[u]=  w[u]*number;//multibly the array with the number                              
        System.out.println(Arrays.toString(finalSum));}//print the array
    }
    

    Notice how that you are writing out the result every time, but then at the next run of the loop, you are overwriting the result, this is where you need the 2d array as in

    double[][] finalSum = new int[w.length][];
    
    for(int i = 0; i < w.length; i++){
        for (int u=0;u<w.length;u++){
                finalSum[i][u]=  w[u]*number;//multibly the array with the number                              
                System.out.println(Arrays.toString(finalSum));}//print the array
        }
    }
    

    now every row of the array should be saved, in the 2d array, and then it can be accesed and summed as described below

    EDITED now adding columns as requested, still assuming it as a 2d array structure

    The issue you are facing as i see it is the way you are summing the data up.

    Made a small example to show how to sum up the column values in a 2d array

    public class NewClass {
        public static void main(String[] args){
            NewClass newClass = new NewClass();
            newClass.method();
        }
    
        public void method(){
            int[][] array = new int[6][6];
            int[] results = new int[6];
            for(int i = 0; i < array.length; i++)
                for(int j = 0; j < array.length; j++) {
                if(i <= array.length/2)
                    array[i][j] = j * i;
                else
                    array[i][j] = j+i;
                }
            for(int i =0; i < results.length; i++)
            {
                for(int j = 0; j < array.length; j++)
                {
                    results[i] += array[j][i];
                }
            }
            System.out.println("Array" + Arrays.deepToString(array));
            System.out.println("results: " + Arrays.toString(results));
        }
    }
    

    This produces an output like this

    Array[[0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5], [0, 2, 4, 6, 8, 10], [0, 3, 6, 9, 12, 15], [4, 5, 6, 7, 8, 9], [5, 6, 7, 8, 9, 10]]

    results: [9, 17, 25, 33, 41, 49]

    Note that a TreeMap is also a set of 2 list when the value contains a list/array so a similar aproach based on the rest of your code is apliable