0

UPDATE: i modified the hashCode, now is a little better. :) but the problem is still there... :(

so, and modified my method too:

private Node getMinNode()
{

    int min = 9999999;   //value
    Node minNode = new Node(); //key,index

    for (Node key : this.distances.keySet()) {

        int minOfNode= this.distances.get(key);

        System.out.println("Key "+ key +", Value = " + minOfNode);

        if(minOfNode<min)
        {
           min= minOfNode;
           minNode= key;
           System.out.println("Key minNode = " + minNode + ", Value minNode = " + minOfNode);

        }
    }

    return minNode;

} 

and there is the output: (the whitespace(, ,) in the lines would be an empty char) and this is not the end... this is only the first few row...

   Key 66601, 3546492, 3546493, 228, f, Value = 9999999
   Key 77393, 3628185, 3628186, 64, t, Value = 9999999
   Key 0, 0, 0, 0, , Value = 0 
   Key minNode = 0, 0, 0, 0, , Value minNode = 0
   Key 66601, 3546492, 3546493, 228, f, Value = 9999999 
   Key 77393, 3628185, 3628186, 64, t, Value = 9999999 

(note: the element with 'Key 0, 0, 0, 0, , Value = 0' does exists, this is the startNode) in that constructor the attribs are initialized with the zeros, i know, so my main problem is, why is that happening? they are passed rigth out of the if(), but in the if() they arent i hope it's clearer what is my problem. :)

5
  • key and minOfNode are not changed in the loop, are you sure the two printlns don't print the same message? Can you show us the output? Also, you might want to slightly modify the second println, so that they are better distinguishable. Commented Feb 12, 2013 at 13:40
  • i was wrong, it prints in the first println this too: Key = 0, 0, 0, 0, , Value = 9999999 , hundreds of rows long..., and it doesnt enter in the if() too Commented Feb 12, 2013 at 13:50
  • Update your question. You have to give us more information if you want help. Commented Feb 12, 2013 at 14:20
  • note: the element with 'Key 0, 0, 0, 0, , Value = 0' does exists, this is the startNode Commented Feb 12, 2013 at 14:48
  • The output looks reasonable (maybe except that the value is always 99999, check that your map is correctly filled here). You are looking for the closest node, which is 0,0,0,0 at distance 0, so the algorithm works as designed. Commented Feb 13, 2013 at 14:21

2 Answers 2

1

First of all storing min and minNode - isn't quite good practice, they aren't necessary.
Next, passing an entire Map to method is better than using global variables (of cource if u trully don't neet it)
Map.keySet() - returns a list of keys, seems like u need Map.getEntrySet() method, to retrieve all 'nodes' (in usual Map they are called Entry)

your code should be like this:

    private Map.Entry getMinNode(Map<String, Integer> distances) {
    Map.Entry minNode = null;

    for (Map.Entry node : distances.entrySet()) {
        System.out.println("Key = " + node.getKey()+ ", Value = " + node.getValue());

        if (minNode == null || node.getValue() < minNode.getValue()) {
           minNode = node;
        }
    }
    return minNode;
}

You should look at JavaDoc http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your reply! it would be a simple MinSearch, but i need to get that Node (only Node) which belongs to the lowest int value. but i dont understand when i make a another instance of the 'Node key' like Node index = key; it's good, but when i passed it through the 'Node key' in the if(), like minNode= key; it doesnt work, and prints out stupids. Why is that happening?
1

First, @StrekoZ is partly right. If you iterated over the entrySet, there would be no need to call get(key).

But I'm not sure what is causing your problem. A couple of theories:

  • The distance values stored in the map really are zero; i.e. the problem is in whatever is creating the entries.

  • Your Key class does not implement equals(Object) and hashCode() correctly.

  • Your Key objects are mutable, and something is mutating them in a way that is changing their hashcode values while they are in the map.

The last two could both cause keys to show up in the keySet, but to be missing when you do a get using them.

UPDATE - the output in your updated question is most consistent with my first theory; i.e. the distance values in the map are incorrect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.