|
-ben
What is happening is that you are creating all those messages so close together that their when field or whatever you call it is always the same. A Map can only have one copy of each “K”, so you are “putting” all your different “V”s for the same “K”, and the later “V”s are overwriting the earlier ones. I got it to work by making two changes to your main method:[campbell@campbell-Inspiron-1110 java]$ java ChatModelis Ben in testModel? true
is Lucas in testModel? true
is Jack in testModel? true
is Bob in testModel? true
is message1 in testModel --> { Bob } ? true
is message2 in testModel --> { Ben } ? false
is message3 in testModel --> { Bob } ? true
is message4 in testModel --> { Jack } ? false
is message5 in testModel --> { Bob } ? true
is message6 in testModel --> { Jack } ? false
is message7 in testModel --> { Ben } ? false
is message8 in testModel --> { Lucas } ? false
is message9 in testModel --> { Ben } ? false
Receiver: Bob
Messsage: Hello World!
Receiver: Bob
Messsage: How are you?
Receiver: Bob
Messsage: What's up
Receiver: Bob
Messsage: nothing
Campbell Ritchie wrote:
Why have you got a TreeMap<String, ChatMessage> rather than TreeMap<Date, ChatMessage>? The java.util.Date class appears to have properly-overridden equals() and hashCode() methods, so you could use a Date. Beware: since Date has setXXX methods, albeit deprecated, and the sent field isn7#x2019;t final, you have at least two possible ways to try to get a message from that Map with a different Date from that which it was “put” in with, and as you doubtless know, you won’t find anything in a Map if that happens.
n output. You appear to be adding messages all right, and the rest of the code looks good. I shall have to try executing your code to see what happens.
Jeff Verdegan wrote:Ben, please BeForthrightWhenCrossPostingToOtherSites(⇐click) so that people don't waste their time duplicating others' responses.
https://forums.oracle.com/forums/thread.jspa?threadID=2338022&tstart=0
https://coderanch.com/t/565563/java/java/some-light-please
Legend has it that if you rub the right tiny ad, a genie comes out.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|