Data loading Hash Map I'm trying to improve my code's performance, and I've found an area where the execution time is being significantly increased. I've had a look through the code but I can't see how I can improve it.

AddData gets called over 200,000 times.

public void AddData(String label, int fromid, int toid, double distance, boolean oneway, int speedlimit) 
    {     
        currentRoad = new Edge(label, fromid, toid, distance, speedlimit, oneway);

        tempVertex = allVertices.GetValue(fromid);
        tempVertex.addEdge(currentRoad);         
        allEdges.AddItem(currentRoad);  
        if (!oneway)
        {
            currentRoad = new Edge(label, toid, fromid, distance, speedlimit, oneway);
            tempVertex = allVertices.GetValue(toid);
            tempVertex.addEdge(currentRoad);
        }         
    }


The method within the AddData function that takes a large proportion of the time is allEdges.AddItem().

allEdges is a Hash Map containing vectors of Edges. (Edges is a class).

Here is my implementation of the Hash Map AddItem function:

public void AddItem(Edge value)
  {
       if ((noofitems/data.length) &gt; 0.7)
       {
           long time = System.nanoTime();
           EdgeHashPair[] newMap = new EdgeHashPair[data.length * multiplier];  
           multiplier = 8;
            for (EdgeHashPair oldMap1 : data) 
            {
                if (oldMap1 != null) 
                {
                    int index = HashFunction(oldMap1.data.GetItem(0).label);
                    int increment = 1;
                    index = index % newMap.length;
                    boolean inserted = false;
                    while (!inserted) 
                    {
                        if (newMap[index] == null) 
                        {
                            newMap[index] = oldMap1;
                            inserted = true;
                        } 
                        else                        
                        {                            
                            index = index + (increment&lt;&lt;1);
                            index = index % newMap.length;
                        }
                    }      
                }
            }
           data = newMap; 
           System.out.println("Hash map resizing took: " + ((System.nanoTime() - time)/1000000) + "ms");
       }


        int index=HashFunction(value.label);
        index = index % data.length;        
        int increment = 1;
        boolean inserted = false;
        while (!inserted)
        {                  
            if (data[index] == null)
            {
                data[index] = new EdgeHashPair();
                data[index].addItem(value);
                noofitems++;
                inserted = true;
            }
            else
            {
                if (data[index].data.GetItem(0).label.compareTo(value.label) == 0)
                {
                    data[index].addItem(value);
                    inserted = true;
                    noitems++;
                }
                else
                {
                    index = index + (increment&lt;&lt;1);
                    index = index % data.length;
                }
            }           
        }   
  }  


Hash function:

      private int HashFunction(String key)
      {
        // Task 1 code: Hash the key and return a long value
        int code = 29;     
        for (int i=0; i &lt; key.length(); i++) 
        {
            code = code*53+(key.charAt(i));
        }    
        return (code &lt; 0 ? -code : code);
  }


From timings I've done, the loading of data currently takes around 50% of my execution time (around 1000ms), which I feel is extremely high and would love to be able to reduce this.

Edit:
The program is a navigation application, it stores edges and vertices and then calculates many routes.
Let's talk about this bit of code.

    int index=HashFunction(value.label);
    index = index % data.length;        
    int increment = 1;
    boolean inserted = false;
    while (!inserted)
    {                  
        if (data[index] == null)
        {
            data[index] = new EdgeHashPair();
            data[index].addItem(value);
            noofitems++;
            inserted = true;
        }
        else
        {
            if (data[index].data.GetItem(0).label.compareTo(value.label) == 0)
            {
                data[index].addItem(value);
                inserted = true;
                noitems++;
            }
            else
            {
                index = index + (increment&lt;&lt;1);
                index = index % data.length;
            }
        }           
    }


First, you have an else block in here...

        else
        {
            if (data[index].data.GetItem(0).label.compareTo(value.label) == 0)
            {
                data[index].addItem(value);
                inserted = true;
                noitems++;
            }
            else
            {
                index = index + (increment&lt;&lt;1);
                index = index % data.length;
            }
        }


... containing only an if-elseif chain. Merge these as such:

        else if (data[index].data.GetItem(0).label.compareTo(value.label) == 0)
        {
            data[index].addItem(value);
            inserted = true;
            noitems++;
        }
        else
        {
            index = index + (increment&lt;&lt;1);
            index = index % data.length;
        }


Second, you only use increment in a constant expression (increment&lt;&lt;1). Consider replacing this with the raw constant instead.

Third, you always loop at least once. So replace your while loop with a do-while loop.



Now for some design issues that could lead to reduced performance...

                int index = HashFunction(oldMap1.data.GetItem(0).label);

    int index=HashFunction(value.label);


When you resize the map, you rehash all the entries, even although their values didn't change. Store the calculated hash in the item.

    for (int i=0; i &lt; key.length(); i++) 
    {
        code = code*53+(key.charAt(i));
    }


When you hash the key, you do so via for-loop over String.length() and String.charAt. These are two function calls, and String.charAt continuously checks the length. Consider declaring a char array of length key.length(), then setting it with String.getChars. I'm assuming you have keys of a decent length here, and not very short. Do several performance tests for this, at different times and with different sets of data.



Performance-tuning your code is likely to be hard without semantics surrounding your code and timing information. See if there's ways to profile which functions take longest... on a more granular level than just this function.