APISonar


java.util.Hashtable.putIfAbsent

> java > util > Hashtable > putIfAbsent
java APIs util APIs Hashtable APIs putIfAbsent APIs

Example 1
public void whenPutifAbsent_thenNotRewritten() {

        Hashtable<Word, String> table = new Hashtable<Word, String>();
        table.put(new Word("cat"), "a small domesticated carnivorous mammal");

        String definition = "an animal";
        // old way
        /* if (!table.containsKey(new Word("cat"))) {
            table.put(new Word("cat"), definition);
        }*/
        // new way
        table.putIfAbsent(new Word("cat"), definition);

        assertThat(table.get(new Word("cat")), is("a small domesticated carnivorous mammal"));
    }