0

I am adding data into HashMap where node is an object with variables index and successor.

private static HashMap <Integer, node> peerList = new HashMap<Integer, node>();

public void generateFingerTable (int node_position) {

            chordSize = chord.initChordSize;        
            chord chord = new chord();  

        //create new node and add to map
        node newPeer = new node();
        peerList.put(node_position, newPeer);

        for (int i=0; i<chordSize; i++) {

            int temp = i+1;

            newPeer.index = new int [chordSize];
            newPeer.successor = new int [chordSize];

            int temp1 = node_position + (int)Math.pow(2, temp-1) % chord.getChordSize();

            peerList.get(node_position).index[i] = temp;                
            peerList.get(node_position).successor[i] = temp1;

            System.out.println ("Index: "  + newPeer.index[i] + "\n" + "Successor: " + 
                    newPeer.successor[i]);          
        }
}

public void printFingerTable() {

        for (Map.Entry<Integer, node> m : peerList.entrySet()) {
            System.out.println ("Peer " + m.getKey() + " with Index: " + m.getValue().getIndex() + " Successor: " +
                                    m.getValue().getSuccessor());
        }

When I print the Hash details, the result shows Index: [0,0,0,0,5] , Successor:[0,0,0,0,16] which means the previously added elements gets replaced and only the last element is saved in Hashmap.

The intended result should be Index [1,2,3,4,5], Successor: [1,2,4,8,16]. How can I amend this so the data don't get replaced?

user4914916
  • 3
  • 1
  • 4
  • 1
    Hi `HashMap` wont allow Duplicates whenever it finds duplicate key it will replace that with latest one. – Rookie007 May 19 '15 at 06:58

2 Answers2

1

You initialize the index and successor arrays in each iteration of the loop, so only the value of the last index remains in the end, and the others are 0.

You should initialize the arrays before the loop.

Change the code to :

public void generateFingerTable (int node_position) {

        chordSize = chord.initChordSize;        
        chord chord = new chord();  

        //create new node and add to map
        node newPeer = new node();
        peerList.put(node_position, newPeer);

        newPeer.index = new int [chordSize];
        newPeer.successor = new int [chordSize];
        for (int i=0; i<chordSize; i++) {
            int temp = i+1; 
            int temp1 = node_position + (int)Math.pow(2, temp-1) % chord.getChordSize();
            peerList.get(node_position).index[i] = temp;                
            peerList.get(node_position).successor[i] = temp1;

            System.out.println ("Index: "  + newPeer.index[i] + "\n" + "Successor: " + 
                    newPeer.successor[i]);          
        }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
0

I think you should use a different data-type or structure than HashMap as HashMaps do not guarantee order. I am pointing this out as your code peerList.put(node_position, newPeer); seems to imply you are setting the position of your object in your HashMap but that is not the case. I only say this because you are just using the variable called node_postion to key or hash where your data object will live in your HashMap. See this link for more details.

Difference between HashMap, LinkedHashMap and TreeMap

Community
  • 1
  • 1
RandomNumberFun
  • 638
  • 2
  • 8
  • 25