简体   繁体   English

HashMap对象数组数据被替换

[英]HashMap Object array data gets replaced

I am adding data into HashMap where node is an object with variables index and successor. 我将数据添加到HashMap中,其中node是具有变量index和后继变量的对象。

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. 当我打印哈希详细信息时,结果显示索引:[0,0,0,0,5],后继者:[0,0,0,0,16]这意味着先前添加的元素将被替换,而只有最后一个元素保存在Hashmap中。

The intended result should be Index [1,2,3,4,5], Successor: [1,2,4,8,16]. 预期结果应为索引[1,2,3,4,5],后继项:[1,2,4,8,16]。 How can I amend this so the data don't get replaced? 我该如何修改以便不替换数据?

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. 您需要在循环的每次迭代中初始化index数组和successor数组,因此只有最后一个索引的值保留在末尾,其他索引均为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]);          
        }
}

I think you should use a different data-type or structure than HashMap as HashMaps do not guarantee order. 我认为您应该使用与HashMap不同的数据类型或结构,因为HashMaps不能保证顺序。 I am pointing this out as your code peerList.put(node_position, newPeer); 我指出这是您的代码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. 似乎暗示您正在设置对象在HashMap中的位置,但事实并非如此。 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. 我之所以这么说是因为您只是使用名为node_postion的变量来对数据对象将存在于HashMap中的键或哈希值进行操作。 See this link for more details. 有关更多详细信息,请参见此链接。

Difference between HashMap, LinkedHashMap and TreeMap HashMap,LinkedHashMap和TreeMap之间的区别

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM