Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not with conventional maps.</p> <p>"abc" is a distinct string from "ABC", their hashcodes are different and their equals() methods will return false with respect to each other.</p> <p>The simplest solution is to simply convert all inputs to uppercase (or lowercase) before inserting/checking. You could even write your own <code>Map</code> wrapper that would do this to ensure consistency.</p> <p>If you want to maintain the case of the key as provided, but with case-insensitive comparison, you could look into using a <a href="http://java.sun.com/javase/6/docs/api/java/util/TreeMap.html" rel="noreferrer">TreeMap</a> and supplying your own Comparator that will compare case-insensitively. However, think hard before going down this route as you <em>will</em> end up with some irreconcilable inconsistencies - if someone calls <code>map.put("abc", 1)</code> then <code>map.put("ABC", 2)</code>, what case is the key stored in the map? Can you even make this make sense? Are you comfortable with the fact that if someone wraps your map in a standard e.g. <code>HashMap</code> you'll lose functionality? Or that if someone happens to be iterating through your keyset anyway, and does their own quick "contains" check by using <code>equals()</code> you'll get inconsistent results? There will be lots of other cases like this too. <strong>Note that you're violating the contract of Map</strong> by doing this (as key equality is <a href="http://java.sun.com/javase/6/docs/api/java/util/Map.html#containsKey(java.lang.Object)" rel="noreferrer">defined in terms of the equals() method on the keys</a>) so it's really not workable in any sense.</p> <p>Maintaining a strict uppercase map is <strong>much</strong> easier to work with and maintain, and has the advantage of actually being a legal Map implementation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload