2013-04-02 4 views
0

저는 프로그래밍 프로젝트를 진행하면서 일부 객체를 목록에 저장하려고하지만 중복을 제거 할 수 없습니다. 목록에 중복 객체가 있습니다.

내 오브젝트

ND = nodeAddress16 = 0x10,0x03, nodeAddress64 = 0x00,0x13,0xa2,0x00,0x40,0x6f, 0x8d, 0xfc, RSSI = -47, nodeIdentifier = [0x10,0x03 인 ]

코드는 스레드 안에 있으므로 코드가 루핑됩니다. handleAtCommandResponse이 같은 응답으로 여러 번 호출되는 이유를 확인하기 위해 시스템의 나머지 부분을 이해하지 않고

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); 
    } 
} 

답변

0

는 중복 객체를 저장 유지하기 위해 목록 대신 HashSet 같은 Set 구현을 사용할 수 있습니다. hashCode()을 구현하려면 NodeDiscover가 필요할 수 있습니다.

Set은 중복 개체를 허용하지 않습니다. 동일한 개체 (오히려 hashCode()이 집합의 다른 개체의 개체와 같음)를 사용하여 put()을 호출하면 첫 번째 인스턴스가 두 번째 인스턴스로 바뀝니다.