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?
>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!");
}