tags:

views:

159

answers:

2

Please refer to the code below. The keys "H_0000001" and "H_0000002" in the hashmaps HstTime and HstName get overwritten by other unique keys."H_000002" get overwritten by "H_00000010" and "H_0000001" gets overwritten by "H_00000012".

package JavaTutes;

import java.util.*;

public class TCProcessData 
{   
        String Unit1HST1ID = "H_0000001";
        String Unit1HST2ID = "H_0000002";
        String Unit1HST3ID = "H_0000003";
        String Unit1HST4ID = "H_0000004";
        String Unit2HST1ID = "H_0000005";
        String Unit2HST2ID = "H_0000006";
        String Unit3HST1ID = "H_0000007";
        String Unit3HST2ID = "H_0000008";
        String Unit3HST3ID = "H_0000009";
        String Unit3HST4ID = "H_00000010";
        String Unit3HST5ID = "H_00000011";
        String Unit3HST6ID = "H_00000012";
        String Unit4HST1ID = "H_00000013";
        String Unit4HST2ID = "H_00000014";
        String Unit4HST3ID = "H_00000015";
        String Unit4HST4ID = "H_00000016";
        String Unit5HST1ID = "H_00000017";
        String Unit5HST2ID = "H_00000018";
        String Unit5HST3ID = "H_00000019";
        String Unit5HST4ID = "H_00000020";
        String Unit5HST5ID = "H_00000021";
        String Unit5HST6ID = "H_00000022";
        String Unit5HST7ID = "H_00000023";
        String Unit5HST8ID = "H_00000024";
        String Unit5HST9ID = "H_00000025";
        String Unit6HST1ID = "H_00000026";
        String Unit6HST2ID = "H_00000027";
        String Unit6HST3ID = "H_00000028";
        String Unit6HST4ID = "H_00000029";

        String Unit1HST1Name = "Some Name 1";
        String Unit1HST2Name = "Some Name 2";
        String Unit1HST3Name = "Some Name 3";
        String Unit1HST4Name = "Some Name 4";
        String Unit2HST1Name = "Some Name 5";
        String Unit2HST2Name = "Some Name 6";
        String Unit3HST1Name = "Some Name 7";
        String Unit3HST2Name = "Some Name 8";
        String Unit3HST3Name = "Some Name 9";
        String Unit3HST4Name = "Some Name 10";
        String Unit3HST5Name = "Some Name 11";
        String Unit3HST6Name = "Some Name 12";
        String Unit4HST1Name = "Some Name 13";
        String Unit4HST2Name = "Some Name 14";
        String Unit4HST3Name = "Some Name 15";
        String Unit4HST4Name = "Some Name 16";
        String Unit5HST1Name = "Some Name 17";
        String Unit5HST2Name = "Some Name 18";
        String Unit5HST3Name = "Some Name 19";
        String Unit5HST4Name = "Some Name 20";
        String Unit5HST5Name = "Some Name 21";
        String Unit5HST6Name = "Some Name 22";
        String Unit5HST7Name = "Some Name 23";
        String Unit5HST8Name = "Some Name 24";
        String Unit5HST9Name = "Some Name 25";
        String Unit6HST1Name = "Some Name 26";
        String Unit6HST2Name = "Some Name 27";
        String Unit6HST3Name = "Some Name 28";
        String Unit6HST4Name = "Some Name 29";

        String Unit1HST1Time = "4.57";
        String Unit1HST2Time = "2.34";
        String Unit1HST3Time = "1.56";
        String Unit1HST4Time = "4.23";
        String Unit2HST1Time = "2.78";
        String Unit2HST2Time = "4.21";
        String Unit3HST1Time = "4.34";
        String Unit3HST2Time = "3.45";
        String Unit3HST3Time = "2.56";
        String Unit3HST4Time = "6.67";
        String Unit3HST5Time = "7.78";
        String Unit3HST6Time = "5.89";
        String Unit4HST1Time = "2.12";
        String Unit4HST2Time = "1.23";
        String Unit4HST3Time = "3.34";
        String Unit4HST4Time = "4.45";
        String Unit5HST1Time = "4.56";
        String Unit5HST2Time = "5.67";
        String Unit5HST3Time = "3.78";
        String Unit5HST4Time = "3.89";
        String Unit5HST5Time = "2.89";
        String Unit5HST6Time = "2.78";
        String Unit5HST7Time = "6.12";
        String Unit5HST8Time = "7.23";
        String Unit5HST9Time = "3.56";
        String Unit6HST1Time = "4.67";
        String Unit6HST2Time = "3.78";
        String Unit6HST3Time = "3.78";
        String Unit6HST4Time = "4.12";

        String Unit1ID = "U_000001";
        String Unit2ID = "U_000002";
        String Unit3ID = "U_000003";
        String Unit4ID = "U_000004";
        String Unit5ID = "U_000005";
        String Unit6ID = "U_000006";

        String Unit1Name = "Some Unit1";
        String Unit2Name = "Some Unit2";
        String Unit3Name = "Some Unit3";
        String Unit4Name = "Some Unit4";
        String Unit5Name = "Some Unit5";
        String Unit6Name = "Some Unit6";

        String[] unitItemID = {Unit1ID, Unit2ID, Unit3ID,
                Unit4ID, Unit5ID, Unit6ID};
        String[] unitItemName = {Unit1Name, Unit2Name,
            Unit3Name, Unit4Name, Unit5Name, Unit6Name};

        private String[] listOfUnits = null;
        private String[] hstIDs = null;
        private Double[] hstTimes = null;
        private Double unitTime = 0.00;
        String unitID = null;
        private double hstTime = 0.00;
        //hashmap key - hstID. value is unit id
        private static HashMap<String, String[]> UnitChildren = new HashMap<String, String[]>();
        //hashmap key - hst id,value is hst time
        private HashMap<String, Double> HstTime = new HashMap<String, Double>();
        //hashmap key - hst id,value is hst name
        private HashMap<String, String> HstName = new HashMap<String, String>();    

        public TCProcessData()
        {

            String[] unit1HSTID = {this.Unit1HST1ID, this.Unit1HST2ID,
                    this.Unit1HST3ID, this.Unit1HST4ID};
            String[] unit2HSTID = {this.Unit2HST1ID, this.Unit2HST2ID};
            String[] unit3HSTID = {this.Unit3HST1ID, this.Unit3HST2ID,
                    this.Unit3HST3ID, this.Unit3HST4ID, this.Unit3HST5ID, this.Unit3HST6ID};
            String[] unit4HSTID = {this.Unit4HST1ID, this.Unit4HST2ID,
                    this.Unit4HST3ID, this.Unit4HST4ID};
            String[] unit5HSTID = {this.Unit5HST1ID, this.Unit5HST2ID,
                    this.Unit5HST3ID, this.Unit5HST4ID, this.Unit5HST5ID, this.Unit5HST6ID,
                    this.Unit5HST7ID, this.Unit5HST8ID, this.Unit5HST9ID};
            String[] unit6HSTID = {this.Unit6HST1ID, this.Unit6HST2ID,
                    this.Unit6HST3ID, this.Unit6HST4ID};

            String[] unit1HSTName = {this.Unit1HST1Name, this.Unit1HST2Name,
                    this.Unit1HST3Name, this.Unit1HST4Name};
            String[] unit2HSTName = {this.Unit2HST1Name, this.Unit2HST2Name};
            String[] unit3HSTName = {this.Unit3HST1Name, this.Unit3HST2Name,
                    this.Unit3HST3Name, this.Unit3HST4Name, this.Unit3HST5Name, this.Unit3HST6Name};
            String[] unit4HSTName = {this.Unit4HST1Name, this.Unit4HST2Name,
                    this.Unit4HST3Name, this.Unit4HST4Name};
            String[] unit5HSTName = {this.Unit5HST1Name, this.Unit5HST2Name,
                    this.Unit5HST3Name, this.Unit5HST4Name, this.Unit5HST5Name, this.Unit5HST6Name,
                    this.Unit5HST7Name, this.Unit5HST8Name, this.Unit5HST9Name};
            String[] unit6HSTName = {this.Unit6HST1Name, this.Unit6HST2Name,
                    this.Unit6HST3Name, this.Unit6HST4Name};

            String[] unit1HSTTime = {this.Unit1HST1Time, this.Unit1HST2Time,
                    this.Unit1HST3Time, this.Unit1HST4Time};
            String[] unit2HSTTime = {this.Unit2HST1Time, this.Unit2HST2Time};
            String[] unit3HSTTime = {this.Unit3HST1Time, this.Unit3HST2Time,
                    this.Unit3HST3Time, this.Unit3HST4Time, this.Unit3HST5Time, this.Unit3HST6Time};
            String[] unit4HSTTime = {this.Unit4HST1Time, this.Unit4HST2Time,
                    this.Unit4HST3Time, this.Unit4HST4Time};
            String[] unit5HSTTime = {this.Unit5HST1Time, this.Unit5HST2Time,
                    this.Unit5HST3Time, this.Unit5HST4Time, this.Unit5HST5Time, this.Unit5HST6Time,
                    this.Unit5HST7Time, this.Unit5HST8Time, this.Unit5HST9Time};
            String[] unit6HSTTime = {this.Unit6HST1Time, this.Unit6HST2Time,
                    this.Unit6HST3Time, this.Unit6HST4Time};

                        UnitChildren.put(this.Unit1ID, unit1HSTID);
            for (int i = 0; i<unit1HSTID.length; i++)
            {
                hstTime = Double.valueOf(unit1HSTTime[i]);
                HstTime.put(unit1HSTID[i], hstTime);
                HstName.put(unit1HSTID[i], unit1HSTName[i]);
            }
            UnitChildren.put( Unit2ID, unit2HSTID );
            for (int i = 0; i<unit2HSTID.length; i++)
            {           
                hstTime = Double.valueOf(unit2HSTTime[i]);
                HstTime.put(unit2HSTID[i], hstTime);
                HstName.put(unit2HSTID[i], unit2HSTName[i]);
            }
            UnitChildren.put(Unit3ID, unit3HSTID);
            for (int i = 0; i<unit3HSTID.length; i++)
            {           
                hstTime = Double.valueOf(unit3HSTTime[i]);
                HstTime.put(unit3HSTID[i], hstTime);
                HstName.put(unit3HSTID[i], unit3HSTName[i]);
            }
            UnitChildren.put( Unit4ID, unit4HSTID);
            for (int i = 0; i<unit4HSTID.length; i++)
            {           
                hstTime = Double.valueOf(unit4HSTTime[i]);
                HstTime.put(unit4HSTID[i], hstTime);
                HstName.put(unit4HSTID[i], unit4HSTName[i]);
            }
            UnitChildren.put(Unit5ID, unit5HSTID);
            for (int i = 0; i<unit5HSTID.length; i++)
            {           
                hstTime = Double.valueOf(unit5HSTTime[i]);
                HstTime.put(unit5HSTID[i], hstTime);
                HstName.put(unit5HSTID[i], unit5HSTName[i]);
            }
            UnitChildren.put(Unit6ID, unit6HSTID);
            for (int i = 0; i<unit6HSTID.length; i++)
            {           
                hstTime = Double.valueOf(unit6HSTTime[i]);
                HstTime.put(unit6HSTID[i], hstTime);
                HstName.put(unit6HSTID[i], unit6HSTName[i]);
            }       
        }

    public int getNoOfRows()
    {
        return unitItemID.length;
    }
    public String[] getUnitIDs()
    {
        return unitItemID;
    }
    public String getUnitID(int row)
    {
        return unitItemID[row];
    }   
    public String[] getUnitTitles()
    {
        return listOfUnits;
    }   
    public HashMap<String, String[]> getUnitChildren()
    {
        return UnitChildren;
    }   
    public HashMap<String, Double> getHstTime()
    {
        return HstTime;
    }   
    public HashMap<String, String> getHstName()
    {
        return HstName;
    }
     public Double getUnitTime(int row)
    {
            this.unitID = this.getUnitID(row);
            hstIDs = this.getUnitChildren().get(unitID);
            for (int i=0; i<hstIDs.length; i++)
            {
                hstTimes[i] = this.getHstTime().get(hstIDs[i]);
                unitTime = unitTime + hstTimes[i];
            }
            return unitTime;    
    }
    public static void main(String[] args)
    {
        TCProcessData someData = new TCProcessData();
        int rows = someData.getNoOfRows();
        double unit1time = someData.getUnitTime(0);
        double unit3time = someData.getUnitTime(2);
        System.out.println(rows);
        System.out.println(unit1time);
        System.out.println(unit3time);
    }


}
A: 

When I run this code I get a NullPointerException because you are trying to insert data into the hstTimes array in your getUnitTime method, but you never instantiate the array.

Also HstTime contains 29 keys, which is consistant with the number of members of all your unitnHSTID arrays, so I'm not sure what issue you are seeing.

Jon Freedman
Hello.... I don't get the NullPointerException ? Also yes the HstTime hashmap size shows as 29...which is consistent but it only shows 27keys and in debug mode i saw the 2keys that i mentioned above get overwritten
akrrs999
@akrrs999: you didn't look at the `HashMap` correctly. Two of the 27 entries you see will have non-`null` `next` values (i.e. they have a collision and the other entry/entries) will be stored behind that reference.
Joachim Sauer
Yes..true but when I try to retrieve the value for key "H_0000001" using the getUnitTime method - it cannot find it ? am I doing something wrong ?....thx.
akrrs999
If I comment out the line `hstTimes[i] = this.getHstTime().get(hstIDs[i]);` and move `unitTime` from an instance variable into the `getUnitTime` method and change the append code to `unitTime += this.getHstTime().get(hstIDs[i]);` I get a result of `12.700000000000001`
Jon Freedman
Allright....I figured out my mistake with the instantiation. Never realized it was that important....thanks a ton.
akrrs999
+1  A: 

What do you mean by overwritten? Due to the way Java calculates hash codes, the String pairs you mention will end up with the same hash value, but the HashMap will handle those collisions gracefully (by making each bucket a linked list of entries, effectively).

Joachim Sauer
By Overwritten I mean that say the key in HstTime hashmap at index 12 gets written as "H_0000001" with a corresponding value of 4.57. As the program progresses - this key gets overwritten by the key "H_00000011" with a corresponding value of 7.78. The previous key "H_0000001" no longer exists in the Hashmap.
akrrs999
i guess....i made a mistake....which Jon Freedman pointed out that i did not look at the hashmap correctly and missed out on the 'next' values....
akrrs999
sorry..pointed out by Joachim Sauer
akrrs999
@akrrs999: as I explained: the old key **does** exist in the `HashMap`, it's simply not the first one in the bucket. Check the `next` value of the new element.
Joachim Sauer
can you recommend how i can rewrite the getUnitTime method with the correct instantiation for hstTimes ?...thx.
akrrs999
Jon Freedman
@akrrs: honestly? no, I can't. Your code is an unreadable mess and I don't have the time to dig into it. For example, you have parallel maps from a "hst ID" to some values. You should only have a single map from the key to some object that encapsulates all values associated with it. That alone should make your code tons more readable.
Joachim Sauer