Search code examples
javacurrency

How can I check if Currency.getAvailableCurrencies() contain my String value?


I need to check if Currency.getAvailableCurrencies() contains my currency which is a string

I've tried this

if(Currency.getAvailableCurrencies().contains("my currency")){
  // do something
}

but that returns false, but it contains "my currency"?


Solution

  • Currency.getAvailableCurrencies() returns Set<Currency> doc, so you are trying to check String in Set of Currency objects which returns false always. You need to create Currency object with the input string, Use Currency.getInstance to get the Currency instance

    if(Currency.getAvailableCurrencies().contains(Currency.getInstance("my currency")))