0

我正在做一个编程项目,我想将一些对象存储在一个列表中,但是我无法摆脱重复项。

这是我的对象

nd = nodeAddress16=0x10,0x03, nodeAddress64=0x00,0x13,0xa2,0x00,0x40,0x6f,0x8d,0xfc, rssi=-47, nodeIdentifier= [0x10,0x03]

代码在线程内,所以代码是循环的。

private void handleAtCommandResponse(XBeeResponse response) {
    //TODO Implement your code here, to handle particular AT command responses, if desired.
    System.out.println("NetworkNode: Received AT Response:"+((AtCommandResponse)response).getCommand());

    if (response.getApiId() == ApiId.AT_RESPONSE) {
        NodeDiscover nd = NodeDiscover.parse((AtCommandResponse)response);
        System.out.println("Node discover response is: " + nd);

        nodeDiscoverList.add(nd); //add to list, but gives duplicates of nd.

        //add to list if not already in it
        //if already in list replace that object with the new object
        //duplicate objects are not allowed ==> only one object in the list can contain a specific address.
        // Only addresses are static values, other values may change over time.


        }
    else {
        log.debug("Ignoring unexpected response: " + response); 
    }
}
4

1 回答 1

0

如果不了解系统的其余部分来帮助确定为什么handleAtCommandResponse会以相同的响应多次调用,请注意,您可以使用像HashSet这样的Set实现而不是 List 来避免存储重复的对象。如果还没有,您可能需要 NodeDiscover 来实现。hashCode()

ASet不允许重复对象。如果您put()使用相同的对象(而不是hashCode()与集合中另一个对象的对象相同的对象)调用两次,则第一个实例将被第二个实例替换。

于 2013-04-07T16:27:36.923 回答