Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should be aware of the fact that your original attempt </p> <pre>int count = map.containsKey(word) ? map.get(word) : 0;</pre> <p>contains two potentially expensive operations on a map, namely <code>containsKey</code> and <code>get</code>. The former performs an operation potentially pretty similar to the latter, so you're doing the same work <em>twice</em>!</p> <p>If you look at the API for Map, <code>get</code> operations usually return <code>null</code> when the map does not contain the requested element.</p> <p>Note that this will make a solution like</p> <pre>map.put( key, map.get(key) + 1 );</pre> <p>dangerous, since it might yield <code>NullPointerException</code>s. You should check for a <code>null</code> first. <p><p> <b>Also note</b>, and this is very important, that <code>HashMap</code>s <em>can</em> contain <code>nulls</code> by definition. So not every returned <code>null</code> says "there is no such element". In this respect, <code>containsKey</code> behaves <em>differently</em> from <code>get</code> in actually telling you <em>whether</em> there is such an element. Refer to the API for details. <p><p> For your case, however, you might not want to distinguish between a stored <code>null</code> and "noSuchElement". If you don't want to permit <code>null</code>s you might prefer a <code>Hashtable</code>. Using a wrapper library as was already proposed in other answers might be a better solution to manual treatment, depending on the complexity of your application.</p> <p>To complete the answer (and I forgot to put that in at first, thanks to the edit function!), the best way of doing it natively, is to <code>get</code> into a <code>final</code> variable, check for <code>null</code> and <code>put</code> it back in with a <code>1</code>. The variable should be <code>final</code> because it's immutable anyway. The compiler might not need this hint, but its clearer that way.</p> <pre> final HashMap map = generateRandomHashMap(); final Object key = fetchSomeKey(); final Integer i = map.get(key); if (i != null) { map.put(i + 1); } else { // do something } </pre> <p>If you do not want to rely on autoboxing, you should say something like <code>map.put(new Integer(1 + i.getValue()));</code> instead.</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. 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