Shaun Aran
Shaun Aran

Reputation: 133

HashMap get(key) method only returns the last entry

So I'm 100% at a loss as to what's causing something like this. Here's the entire section of code that I'm working with

public static void getBestHeuristic(int[] nextParent) {

    int bestHeuristic = 0;

    Random rndm = new Random();
    int randomIndex = 0;
    HashMap<Integer, int[]> childHuristics = new HashMap<Integer, int[]>();
    int k = 0;
    while (k < nextParent.length - 1) {
        for (int i = 0; i < (nextParent.length) - 2; i++) {
            randomIndex = rndm.nextInt(nextParent.length / 5) + i;
            int temp = nextParent[i];
            nextParent[i] = nextParent[randomIndex];
            nextParent[randomIndex] = temp;
        }
        // Assign this random set to the child
        int[] childArray = nextParent;

        int childDistance = 0;

        // Calculate the heuristic of the new current child
        for (int j = 0; j < childArray.length - 1; j++) {
            childDistance += cities[childArray[j]][childArray[j + 1]];
        }

        childHuristics.put(childDistance, childArray);

        // Display the current values added to the HashMap
        System.out.println("Distance: " + childDistance);
        System.out.println(Arrays.toString(childArray));
        System.out.println();

        // Keep track of the lowest value
        if (bestHeuristic == 0) {
            bestHeuristic = childDistance;
        } else if (childDistance < bestHeuristic) {
            bestHeuristic = childDistance;
        }

        k++;
    }
    // Display the lowest distance
    System.out.println();
    System.out.println("The best child was: ");
    System.out.println("Distance: " + bestHeuristic);
    System.out.println(Arrays.toString(childHuristics.get(bestHeuristic)));

Here is the output that I get from this:

Distance: 670
[12, 5, 6, 10, 2, 3, 4, 8, 0, 9, 7, 1, 14, 11, 13]

Distance: 680
[6, 10, 2, 12, 4, 8, 5, 0, 9, 7, 3, 14, 13, 11, 1]

Distance: 611
[2, 12, 6, 4, 5, 8, 9, 7, 0, 10, 3, 13, 14, 11, 1]

Distance: 668
[2, 4, 5, 12, 9, 7, 6, 0, 3, 13, 10, 8, 14, 11, 1]

Distance: 684
[2, 5, 9, 7, 6, 12, 4, 13, 3, 8, 0, 14, 10, 11, 1]

Distance: 634
[2, 9, 5, 12, 6, 7, 4, 8, 3, 14, 0, 11, 13, 10, 1]

Distance: 736
[9, 12, 6, 7, 2, 8, 5, 4, 3, 11, 14, 13, 0, 10, 1]

Distance: 622
[9, 7, 6, 12, 5, 4, 3, 11, 8, 14, 0, 10, 1, 13, 2]

Distance: 585
[9, 7, 5, 6, 4, 11, 3, 12, 8, 0, 10, 13, 14, 1, 2]

Distance: 554
[9, 5, 7, 4, 6, 11, 8, 12, 3, 10, 0, 14, 13, 1, 2]

Distance: 587
[9, 7, 4, 6, 11, 5, 8, 3, 12, 0, 10, 1, 13, 14, 2]

Distance: 575
[7, 4, 6, 11, 8, 9, 5, 12, 3, 1, 0, 10, 14, 13, 2]

Distance: 642
[4, 6, 7, 8, 5, 12, 9, 3, 1, 0, 11, 14, 10, 13, 2]

Distance: 634
[4, 6, 7, 5, 9, 12, 1, 8, 0, 11, 10, 13, 14, 3, 2]


The best child was: 
Distance: 554
[4, 6, 7, 5, 9, 12, 1, 8, 0, 11, 10, 13, 14, 3, 2]

To give some context to all this, basically each number in each array has a distance value associated with them. That's what the distance value of each one represents. The 'cities' array is the 2D array that holds all of those distance values. It looks something like this:

0  29  82  46  68  52  72  42  51  55  29  74  23  72  46  
29   0  55  46  42  43  43  23  23  31  41  51  11  52  21  
82  55   0  68  46  55  23  43  41  29  79  21  64  31  51  
46  46  68   0  82  15  72  31  62  42  21  51  51  43  64  
68  42  46  82   0  74  23  52  21  46  82  58  46  65  23  
52  43  55  15  74   0  61  23  55  31  33  37  51  29  59  
72  43  23  72  23  61   0  42  23  31  77  37  51  46  33  
42  23  43  31  52  23  42  0  33   15  37  33  33  31  37  
51  23  41  62  21  55  23  33   0  29  62  46  29  51  11  
55  31  29  42  46  31  31  15  29   0  51  21  41  23  37  
29  41  79  21  82  33  77  37  62  51   0  65  42  59  61  
74  51  21  51  58  37  37  33  46  21  65   0  61  11  55  
23  11  64  51  46  51  51  33  29  41  42  61   0  62  23  
72  52  31  43  65  29  46  31  51  23  59  11  62   0  59  
46  21  51  64  23  59  33  37  11  37  61  55  23  59   0

The issue I'm running into, is that the 'get(key)' method of the HashMap that I have is not returning the Array associated with the key that I give it.

It says Distance: 554 [4, 6, 7, 5, 9, 12, 1, 8, 0, 11, 10, 13, 14, 3, 2]

When the distance 554 is actually lined to this array: [9, 5, 7, 4, 6, 11, 8, 12, 3, 10, 0, 14, 13, 1, 2]

I don't know if there's something wrong with how I'm putting them into the HashMap or not. I tried using a TreeMap to just force it to be sorted and get the first value, but the result was the same.

Thanks for your time.

Upvotes: 0

Views: 122

Answers (1)

Paul Boddington
Paul Boddington

Reputation: 37645

It's because you are putting the same reference into the map multiple times. If you do

int[] childArray = nextParent.clone();

or

int[] childArray = Arrays.copyOf(nextParent, nextParent.length)

instead, you will use a fresh array each time.

Upvotes: 2

Related Questions