Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Map not checking for values properly

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Map not checking for values properly

    I apparently don't understand how to check for existing values in a Map.

    I have this:
    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?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Map not checking for values properly

    Quote Originally Posted by ober0330 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Map not checking for values properly

    Quote Originally Posted by ober0330 View Post
    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 View Post
    Is that even possible?
    What happened when you tried? :p
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Map not checking for values properly

    Quote Originally Posted by ober0330 View Post
    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 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Map not checking for values properly

    Screw it, I'm just going to concatenate the two items and build an arraylist.

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Map not checking for values properly

    Quote Originally Posted by ober0330 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. #13
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Map not checking for values properly

    Quote Originally Posted by KevinWorkman View Post
    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

  14. #14
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Map not checking for values properly

    Quote Originally Posted by KevinWorkman View Post
    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.

  15. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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.

  18. #18
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Map not checking for values properly

    If its working now, mark this thread as solved.

  20. #20
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Map not checking for values properly

    Done, sorry, forgot about that.

  21. #21
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Map not checking for values properly

    Quote Originally Posted by ober0330 View Post
    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 View Post
    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 View Post
    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 View Post
    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. [SOLVED] compare form values with database values
    By VaniRathna in forum Java Servlet
    Replies: 2
    Last Post: October 24th, 2011, 02:48 AM
  2. Character Values Inside of Number Values
    By bgroenks96 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 2nd, 2011, 08:27 PM
  3. Checking Blurbs
    By 9erNumber16 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 7th, 2011, 07:29 PM
  4. Checking in.
    By Johannes in forum Member Introductions
    Replies: 7
    Last Post: August 13th, 2009, 06:11 AM
  5. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM