0

How do I check if a Key is mapped in a HashMap?

Here is my code:

 currencyMap = new HashMap<String, String>(); currencyMap.put("USD", "US Dollar"); currencyMap.put("EUR", "Euro"); currencyMap.put("GBP", "British Pound Sterling"); currencyMap.put("JPY", "Japanese Yen"); currencyMap.put("CAD", "Canadian Dollar"); currencyMap.put("AUD", "Australian Dollar"); String checkCurrency = "CHF"; // Swiss Franc // List Keys for (String key : currencyMap.keySet() ) { String value = currencyMap.get( key ); System.out.println( key + " = " + value); } 

2 Answers 2

2

Use

boolean isMapped = currentMap.containsKey(checkCurrency); 
Sign up to request clarification or add additional context in comments.

Comments

2

When you have questions like these in the future, try to find the HashMap javadoc and then you will be able to determine very quickly what to use.

In this case you can use HashMap#containsKey(java.lang.Object) or if you want to find if the value is in the HashMap, you can use HashMap#containsValue(java.lang.Object).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.