-1

So basically I have treeMap and I want to find keys in it the thing is that searching for ABC or Abc or aBc or abC or ABc or AbCor aBC it should return true in the containsKey after using some comparator i think.

The thing is that i already tried to covert the String all into lower case and upper case but sometimes i need the key to be like aBC because i need to print the key and printing abc and ABC or ABc are different things.

So do you know another way to do this?

Martim Correia
  • 483
  • 5
  • 16

1 Answers1

1

Use toLower() when adding to the map, and also when searching. But, you'll have to add special handling if you want to be able to store distinct values for keys that differ only in upper/lower case.

If you need to keep the original case you'll have to modify the value object to store it.

You might also want to subclass TreeMap and override the put and get methods to take care of the toLower() calls. Remember to override ALL methods that get or put values.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 1
    Or wrap your keys: `class Key { private final String key; public Key(@Nonnull String key) { this.key = key; } // the original, case sensitive key public getKey() { return key; } public boolean equals(Key other) { return this.key.toLower().equals(other.key.toLower()); } public int hashCode() { return key.toLower().hashCode(); } }` – tgdavies Dec 10 '20 at 20:29