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.
Boolean
andboolean
to demonstrate auto-boxing. The value stored is exactly the same.