0

I have an LinkedHashMap that uses the values from a char array as its key adds integers 1 to the size of the char array as its values:

hmConvert = new LinkedHashMap<Character, Integer>();
for (int m = 0; m < referenceString.length; m++) {
    hmConvert.put(referenceString[m], m);
}

an example of values in the char array would be:

'0', '1', '5', '3', '6', '6', '4', '4', '5', '5', '6', '0', '3', '3', '5'

When i use the get method on the LinkedHashMap it is returns the higest key back, for example:

int test = hmConvert.get(5);
System.out.println(test);

The output would be: 14

I need the output in this case to be the LOWEST value after a GIVEN input value. So if I inputted 4 as the input, the returned value would be 8.

How would I go about doing this.

David Tunnell
  • 7,252
  • 20
  • 66
  • 124

1 Answers1

2

Okay first of all, I assume you mean int test = hmConvert.get('5');, because in your example, it would be null, not 14.

You can accomplish this goal simply by putting the values in backwards (so the first keys are the ones that don't get overwritten). In other words, do this:

for (int m = referenceString.length - 1; m >= 0; m--) {
  hmConvert.put(referenceString[m], m);
}
durron597
  • 31,968
  • 17
  • 99
  • 158
  • Yes, I mean't '5'. Also, this works great, but one last requirement that I haven't been able to figure out. It is now finding the lowest key but how do I find the lowest key after a specific given index? – David Tunnell May 01 '13 at 23:01
  • change `m >= 0` to `m => specificIndex` – durron597 May 01 '13 at 23:03