5

I am practicing using HashMap in JAVA from a tutorial. The sample HashMap implementation code fails to compile with the error:

DictionaryPractice.java:57: error: cannot find symbol
                              shoppingList.replace("Bread", Boolean.FALSE);
symbol:   method replace(String,Boolean)
location: variable shoppingList of type Map<String,Boolean>

Here is the code:

import java.util.HashMap;
import java.util.Map;

public class DictionaryPractice {
    public static void main(String[] args) {
        Map<String, Boolean> shoppingList = new HashMap<String, Boolean>();

        // Put some stuff in dictionary
        shoppingList.put("Ham", true);
        shoppingList.put("Bread", Boolean.TRUE);
        shoppingList.put("Oreos", Boolean.TRUE);
        shoppingList.put("Eggs", Boolean.FALSE);
        shoppingList.put("Sugar", false);

        // Retrieve items
        System.out.println(shoppingList.get("Ham"));
        System.out.println(shoppingList.get("Oreos"));

        // Remove things
        shoppingList.remove("Eggs");

        // Replace values for a certain key
        shoppingList.replace("Bread", Boolean.FALSE);
    }
}

I have read the JavaDocs on the HashMap class, and confirmed that .replace is a valid HashMap method to replace a value for a specified key. However, I keep getting the cannot find symbol error. Your kind help will be appreciated. Sorry for the basic question.
I am using jEdit Text Editor with the Compile plugin installed on a MacOSX Yosemite.

5
  • 2
    you can simply overwrite the the value since the hash map will overwrite the value of an existing key shoppingList.put("Bread", Boolean.FALSE); will do the job. another advice is to not mix types use either Boolean.FALSE or simply false
    – whyn0t
    Aug 15, 2016 at 7:58
  • @whyn0t I think the tutorial uses both Boolean and boolean to demonstrate auto-boxing. The value stored is exactly the same. Aug 15, 2016 at 8:06
  • @RealSkeptic i didn't say that it was wrong, it is just agood programming practice
    – whyn0t
    Aug 15, 2016 at 8:11
  • @whyn0t Thanks for the advice. It is appreciated. Aug 15, 2016 at 8:45
  • @RealSkeptic You're right. The tutorial varied between the reference and primitive types to demonstrate the implicit conversion that take place regardless of what you use (i.e. auto-boxing). Thank you much. Aug 15, 2016 at 8:48

1 Answer 1

5

The method replace(K,V) in the Map interface is a new method introduced in Java 8.

Apparently, you are compiling your code with Java 7 or earlier.

Two possible solutions are:

  1. Download a Java 8 JDK for Mac OS X and use that to compile your code.
  2. Replace the replace with put. The method replace is a convenience method used when you don't want the new value to be placed in the map if the key doesn't have some value beforehand, similar to:

    if ( shoppingList.contains("Bread") ) {
         shoppingList.put("Bread",Boolean.FALSE);
    }
    

    In your case, since you put something for Bread a few lines earlier, replace would be unnecessary - you know that Bread is there. So just use put directly.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.