Map not checking for values properly
I apparently don't understand how to check for existing values in a Map.
I have this:
Code :
private Map<String, String> priorChecks = new LinkedHashMap<String, String>();
boolean checkNew = false;
if(!this.priorChecks.containsKey(node.getName())) {
// brand new object being checked
this.priorChecks.put(node.getName(), type);
checkNew = true;
} else if(!this.priorChecks.get(node.getName()).equals(type)) {
System.out.println("++++++node++++++"+node.getName()+"++++"+this.priorChecks.get(node.getName()));
// this is a new instance of the change, add it and check for notifications
this.priorChecks.put(node.getName(), type);
checkNew = true;
}
And even when the same key comes in with the same value, it adds it again, but under an existing entry. I don't understand why it is doing that?
Re: Map not checking for values properly
If you want help, you'll have to provide an SSCCE that demonstrates the problem. Otherwise we're just guessing at the code we can't see.
Re: Map not checking for values properly
Hi Kevin,
I'm not sure what else you might need. I gave you the code that is causing the problem. I was hoping the problem might just be with the else if portion. Like maybe I'm not checking for existing values properly? I can't give you a fully functional example without rewriting a bunch of stuff.
Re: Map not checking for values properly
The code you posted doesn't really give us much information, and we don't know what it's supposed to do or what it's doing. You've used variables that we have no idea the values of, or if they even matter in your Node class's equals or hashcode methods. So if you want help, an SSCCE really is pretty necessary.
Re: Map not checking for values properly
Ok, so let me ask a more generic question then:
I'm processing a set of objects. Each object can have different states throughout the process. The user can setup notifications so that when an object gets to a specific state, it will trigger a notification.
However, because of the way these objects are processed, the same object might come through with the same state multiple times. Example:
A = on
B = off
C = on
A = off
A = on <- I already saw this state, I don't want to process it again
So my whole point is that I want a way to track what I've already processed. So I created a map and the logic is, if the key isn't in the map, add the key and whatever the value is and send notifications. If the key DOES exist, make sure the key/value pair isn't already there. If the key/value pair exists, ignore the current iteration and do not send notifications. If the key exists but this is a new value for the key, add it to the map and check for notifications.
Now that I'm talking through this, I wonder if my issue is that I can have multiple keys that are the same:
A = on
A = off
B = on
C = off
Can Maps handle the same key with different values?
Re: Map not checking for values properly
Quote:
Originally Posted by
ober0330
Ok, so let me ask a more generic question then:
I'm processing a set of objects. Each object can have different states throughout the process. The user can setup notifications so that when an object gets to a specific state, it will trigger a notification.
However, because of the way these objects are processed, the same object might come through with the same state multiple times. Example:
A = on
B = off
C = on
A = off
A = on <- I already saw this state, I don't want to process it again
So my whole point is that I want a way to track what I've already processed. So I created a map and the logic is, if the key isn't in the map, add the key and whatever the value is and send notifications. If the key DOES exist, make sure the key/value pair isn't already there. If the key/value pair exists, ignore the current iteration and do not send notifications. If the key exists but this is a new value for the key, add it to the map and check for notifications.
Now that I'm talking through this, I wonder if my issue is that I can have multiple keys that are the same:
A = on
A = off
B = on
C = off
Can Maps handle the same key with different values?
I see. A Map might be the way to go here, but you're going to need a List of every value you've seen for each key. Right now you're only storing the most recently seen value for each key.
Re: Map not checking for values properly
So is there a better way to do this? Or are you saying create a map with the value being a list? Is that even possible?
Re: Map not checking for values properly
Quote:
Originally Posted by
ober0330
So is there a better way to do this? Or are you saying create a map with the value being a list?
Creating a Map with a value being a List seems reasonable to me. That doesn't mean there isn't a better way. But it seems reasonable.
Quote:
Originally Posted by
ober0330
Is that even possible?
What happened when you tried? :p
Re: Map not checking for values properly
Could you be more generic in your responses?
If there is a better way, why wouldn't you tell me? I thought the point of this board was to help people, not make philosophical statements about things that could be. I'm not some jerk on here trying to get help with homework. I have a basis in the language and I'm trying to expand my knowledge and use the most efficient objects to achieve my goal.
And I am trying to use a list for the values in the map, but I'm not sure how to add a value to a list when it's not a referenced variable.
Re: Map not checking for values properly
Quote:
Originally Posted by
ober0330
Could you be more generic in your responses?
If there is a better way, why wouldn't you tell me? I thought the point of this board was to help people, not make philosophical statements about things that could be. I'm not some jerk on here trying to get help with homework. I have a basis in the language and I'm trying to expand my knowledge and use the most efficient objects to achieve my goal.
Drop the attitude, right now, or I'm done. I did nothing to warrant that. I told you to use a Map of Lists, which is a pretty specific answer. However, since I don't actually know the details of your situation, I can't guarantee that that's the "best" way to do it, so I was honest about it. If you're going to jump down my throat for that, then my time is better spent elsewhere.
Quote:
Originally Posted by
ober0330
And I am trying to use a list for the values in the map, but I'm not sure how to add a value to a list when it's not a referenced variable.
Well then, I suggest you throw together an SSCCE demonstrating exactly what you're trying, and we can go from there.
Re: Map not checking for values properly
Screw it, I'm just going to concatenate the two items and build an arraylist.
Re: Map not checking for values properly
Quote:
Originally Posted by
ober0330
Screw it, I'm just going to concatenate the two items and build an arraylist.
Suit yourself. I'm still not sure where your attitude is coming from, I thought you were on the right track. I don't think it's outrageous for me to ask to see what you've got so far. I will say, for the record, that concatenating them and using an ArrayList is almost definitely NOT the best way to go.
Re: Map not checking for values properly
Quote:
Originally Posted by
KevinWorkman
Drop the attitude, right now, or I'm done. I did nothing to warrant that. I told you to use a Map of Lists, which is a pretty specific answer. However, since I don't actually know the details of your situation, I can't guarantee that that's the "best" way to do it, so I was honest about it. If you're going to jump down my throat for that, then my time is better spent elsewhere.
You want to talk about attitude? Here's the attitude I get EVERY time I come to this forum:
Me: Post Question
Someone else: Your code tells me nothing and I'm not even going to bother to guess at what could possibly be causing the problem. OH, and here's a link for the PERFECT example of how to post in a forum.
Me: Points to specific line in the code that I think the problem is at, but I don't know how to solve.
Someone else: Generic statement, why haven't you tried it yet, BLAH BLAH BLAH
No one in this forum ever tries to guess at a solution. 95% of the people that respond to threads that I've been involved with assume everyone to be a child in a class that knows absolutely nothing, yet they are not willing to even try to figure out what the problem might be or ask any leading questions.
And I don't know whether it's the culture of the average Java developer or what. In the PHP community that I ran, people are more willing to offer ideas or ask leading questions to get the answers they need to help solve the problem.
Maybe it's me... but I've been posting on boards like this for over a decade so I think I have a general idea of how to post and what to ask.
And I see you're a mod here, so maybe you can take this back to the rest of the admin group here and do one of the following:
1) Ignore me and point and laugh at me
2) Take this seriously and try to change the tone
/rant
Re: Map not checking for values properly
Quote:
Originally Posted by
KevinWorkman
Suit yourself. I'm still not sure where your attitude is coming from, I thought you were on the right track. I don't think it's outrageous for me to ask to see what you've got so far. I will say, for the record, that concatenating them and using an ArrayList is almost definitely NOT the best way to go.
See, there you go again.
"That's definitely NOT the best way to go, but I'm sure as hell not going to give you a better way. Why would I help my fellow programmer become better at what he's doing?"
I'm not the only one with an attitude here.
Re: Map not checking for values properly
I've already explained this to you- I don't KNOW the best way, simply because I don't know the specifics of your situation. Every single context is different. And even if I did know the exact context of your project, that can change 10 times a day, as can your requirements. And even then, there might not be one single best way to go. I gave you a suggestion that I thought was reasonable. I then simply let you know that I wasn't guaranteeing it was the best way to go FOR YOUR SITUATION, WHICH ONLY YOU KNOW. I don't think there is any other way to give advice.
You have to keep in mind that everybody here is working FOR FREE, IN THEIR SPARE TIME. We're doing this because we enjoy helping people learn how to program. However, that doesn't mean we have time to debug every piece of code dumped here. That's why we ask for an SSCCE (also because half the time, people figure it out themselves during the process of putting one together). If you ask a specific question and post an SSCCE demonstrating what you're talking about, you'll get help. If, on the other hand, you post a small piece of the code that doesn't give an accurate picture of what you're doing, and ask a question like "what's the best way to do XYZ", then that's a bit harder to answer. However, I still gave you a reasonable answer, and you still came back with an ongoing tantrum.
I can't imagine any technical forums where that kind of behavior is acceptable. Perhaps you're more in your element with PHP and therefore more able to ask technical questions. But I think I've been pretty reasonable here. It's up to you how you want to proceed.
Re: Map not checking for values properly
You can't even see how frustrating those answers you give might be? Do you even understand what I said?
Obviously the situation can vary greatly and every answer you might give might not be the perfect example every time. But if we all took that perspective, no one would ever give any answers at all.
Obviously I know people respond to these threads for free. But there are hundreds of boards out there exactly like this that don't point that out, as you seem to think you have to.
My point is, you didn't even try to guess at what my problem might be at the beginning. You wanted a perfect example that absolutely replicated my problem but without all of the objects I'm using in my code. Do you know how pointless that is?
Re: Map not checking for values properly
To get back on the problem:
Add some printlns before the if statement to print out the value returned by the getName method and the contents of the priorChecks Map object so you can visually verify what is going on.
Re: Map not checking for values properly
I did that, and I watched it in debug in Eclipse and it's just replacing the key/value with the new value.
Regardless, I changed it and I'm just using a HashSet of concatenated strings now. It's working.
Thanks Norm!
Re: Map not checking for values properly
If its working now, mark this thread as solved.
Re: Map not checking for values properly
Done, sorry, forgot about that.
Re: Map not checking for values properly
Quote:
Originally Posted by
ober0330
You can't even see how frustrating those answers you give might be? Do you even understand what I said?
I understand what you said perfectly. Do you understand what I have said?
Quote:
Originally Posted by
ober0330
Obviously the situation can vary greatly and every answer you might give might not be the perfect example every time. But if we all took that perspective, no one would ever give any answers at all.
I disagree. There's a difference between not giving any answers at all and giving an answer but adding a caveat that you're not guaranteeing that any solution is "best" for an unknown context. I gave you a reasonable approach. And since I didn't also include an SSCCE (which you yourself refused to include) showing you exactly how to do that, you got upset.
Quote:
Originally Posted by
ober0330
Obviously I know people respond to these threads for free. But there are hundreds of boards out there exactly like this that don't point that out, as you seem to think you have to.
I didn't point it out until you overreacted and behaved as if you were entitled to some kind of special treatment. Everybody plays by the same rules here. When I have a question, I include an SSCCE. If I don't get the answer I'm looking for, I ask a more specific question. I don't throw tantrums at people who are trying to help me.
Quote:
Originally Posted by
ober0330
My point is, you didn't even try to guess at what my problem might be at the beginning. You wanted a perfect example that absolutely replicated my problem but without all of the objects I'm using in my code. Do you know how pointless that is?
Why should we have to guess at what your problem is? It's your job to make it easy for us to help you. Why on earth would you want to make it harder for people to help you for free?
I entirely disagree that providing an SSCCE is pointless. It is the opposite of pointless. The reason I wanted an SSCCE was mostly because I wanted to see the Node class. A common problem when using HashMap is overriding equals() but not hashcode(). That can cause strange behaviors. But without seeing your Node class, and an example of how you use that class that I could play with, I'd simply be guessing, which is a huge waste of your time- and mine, and the time of the hundreds of other people here waiting on a reply.
However, your original question did not actually mention the specific problem of wanting to hold multiple values for each key. You said you didn't know how to check for existing values, but then the piece of code you did post actually contained a check for an existing value, which is why I said it was unclear and asked for the SSCCE. Seeing what you were actually trying to do would have gone a long way to help get an answer that wasn't "generic" (even though you labeled your second reply as a generic question, so a generic answer should have been all you hoped for).
I'll repeat myself again- simply concatenating the values is almost definitely not the best way to go. A better way to go would be to use a Map of Lists, as I said before. But it's your code, so feel free to do what you want. But don't complain when people don't tell you the best way to do things when you ignore the advice anyway.