-1

I have this simple code and i figured out that for the last array number the containsKey method returns always false.

int[] indices = new int[] { 1, 3, 5, 7, 9 };

Map<Integer, Integer> seen = new HashMap<>();

for (int i = 0; i < indices.length - 1; i++) {
    seen.put(indices[i], i);
}

All other true except:

System.out.println("!!!!! " + seen.containsKey(9) );

Also with new

int[] { 1, 3, 5, 7 };

All other true except:

System.out.println("!!!!! " + seen.containsKey(7) );

What is the logic behind this?

ddarellis
  • 3,912
  • 3
  • 25
  • 53
  • 2
    You can see the problem for yourself by adding `System.out.println(seen);` -- or just looking at the content of `seen` using a debugger. – slim Apr 05 '17 at 10:40
  • 1
    "What is the logic behind this?" - well, what would _you_ say is the logic behind not adding the last element in the array? – Thomas Apr 05 '17 at 10:42

7 Answers7

6

You don't put the last element of your indices array in your Map.

Change

for (int i = 0; i < indices.length - 1; i++) {
    seen.put(indices[i], i);
}

to

for (int i = 0; i < indices.length; i++) {
    seen.put(indices[i], i);
}
Eran
  • 387,369
  • 54
  • 702
  • 768
6

In the for loop

for (int i = 0; i < indices.length - 1; i++) 

change the condition to i <= indices.length - 1 or other option is to use i < indices.length

In your code, You are adding only upto second last element of the array to the map.

Pooja Arora
  • 574
  • 7
  • 19
1

Look at your for loop:

for (int i = 0; i < indices.length - 1; i++) {

The condition in your for loop is wrong. With < indices.length -1 you actually only put the first 4 keys into the map. It should be either

<= indices.length -1

or

< indices.length
Flurin
  • 681
  • 5
  • 14
0

After you populate the map with this:

    for (int i = 0; i < indices.length - 1; i++) {
        seen.put(indices[i], i);
    }

it will look like:

    {1=0, 3=1, 5=2, 7=3}

so you are asking the map if there is a key with the value 9....

the answer is correct, it doesn't have....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Because in your code loop stared from 0 and your last key is (indices.length -1) .

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
0

Seems you are not adding the last elements, you loop is running till less then length - 1. Simply either remove -1

for (int i = 0; i < indices.length; i++) {
seen.put(indices[i], i);
}

Or use less than equal to

for (int i = 0; i <= indices.length - 1; i++) {
seen.put(indices[i], i);
}

Its not issue with containsKey , get will also not give value to you as last element is never added.

Panther
  • 3,312
  • 9
  • 27
  • 50
0

Just my two cents: this is the functional version that avoid these kinds of problems:

int[] indices = new int[] {1, 3, 5, 7, 9};

Map<Integer, Integer> map = IntStream.range(0, indices.length)
         .mapToObj(i -> {
           return new int[] {i, indices[i]};
         })
         .collect(Collectors.toMap(i -> i[1], i -> i[0]));
freedev
  • 25,946
  • 8
  • 108
  • 125