Java HashMap唯一密钥被覆盖 - 请查看代码和建议

时间:2010-09-16 13:53:46

标签: java hashmap

请参阅以下代码。哈希映射HstTime和HstName中的键“H_0000001”和“H_0000002”被其他唯一键覆盖。“H_000002”被“H_00000010”覆盖,“H_0000001”被“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);
    }


}

2 个答案:

答案 0 :(得分:1)

被覆盖的是什么意思?由于Java计算哈希码的方式,您提到的String对将以相同的哈希值结束,但HashMap将优雅地处理这些冲突(通过使每个桶成为链接的条目列表,有效)。 / p>

答案 1 :(得分:0)

当我运行此代码时,我得到一个NullPointerException因为您试图将数据插入hstTimes方法中的getUnitTime数组中,但您从未实例化该数组。

另外HstTime包含29个密钥,这些密钥与所有unitnHSTID数组的成员数一致,所以我不确定您看到的是什么问题。