I have a bunch of DataOutputStreams in Hashtable outputStreams;. There is a Socket for each of them. I want to loop through them but I want to exclude one particular Socket. I’ve been trying with the following algorithm but it doesn’t seem to work.
for(Enumeration e = outputStreams.elements(); e.hasMoreElements(); ) {
DataOutputStream dout = (DataOutputStream)e.nextElement();
OutputStream sdout = null;
try {
sdout = socket.getOutputStream();
} catch (IOException ie) {
ie.printStackTrace();
}
if (dout != sdout) {
try {
dout.writeUTF(message);
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
It appears that a
Hashtablewill insert a copy of the instance into its backing structure. Andypandy is right about your problem, but then I wondered if a table ofOuputStreams (where you wouldn’t wrap the socket output stream) would fix it and it didn’t.Output:
The socket’s output stream that is inserted into the table has a different address than that of the original output stream. Since there’s no
equalsoverride, there’s no easy way to meaningfully compare streams without having knowledge of their corresponding sockets. You should re-think your design completely, perhaps using the socket descriptor as your key.