Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a string is in a HashMap value

I’m working on a slang dictionary java application and I got stuck at checking a given string is in the hashmap value or not.
For example:

HashMap<String,String> hashList = new HashMap<String,String>();
hashList.put("$","Dollar| money");
hashList.put("$_$","Has money");
if(hashList.containsValue("money")) {
    System.out.println("Found!")
}

But it doesn’t work. Is there a way to find it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

containsValue checks if value that’s equal to the argument is in the map – which isn’t the case here – you’re looking for a value that contains the string "money". You’ll have to resort to iterating over the map’s values. Luckily, Java’s streams will allow you do this as a oneliner:

if (hashList.values().stream().anyMatch(v -> v.contains("money")) {
    System.out.println("Found!");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading