如何检查Key是否映射在HashMap中?

这是我的代码:

    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);
    }

最佳答案

使用

boolean isMapped = currentMap.containsKey(checkCurrency);

07-24 18:37