Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of key's in HashMap having key like 'some value'

Tags:

java

hashmap

In Mysql we can query a table having clause " WHERE name LIKE '%someName%' ", can we have the same functionality with HashMap in java,if so how can we achieve this more efficiently in a less time by not iterating over each element?

like image 999
achuth Avatar asked Apr 17 '14 09:04

achuth


People also ask

Can HashMap have list as key?

Yes you can have ArrayList s as a keys in a hash map, but it is a very bad idea since they are mutable. If you change the ArrayList in any way (or any of its elements), the mapping will basically be lost, since the key won't have the same hashCode as it had when it was inserted.


1 Answers

If you're using Java SE 8 and the new Streams API: there is a filter method which basically is what you are looking for, I think.

e.g. something like (untested!):

myMap.entrySet().stream().filter(entry -> entry.getKey().contains("someName")).map(entry -> entry.getValue()).collect(Collectors.toList());
like image 150
Puce Avatar answered Oct 25 '22 02:10

Puce