I am new to the collections in Java , I have a HashMap<String, List<String>>
. I want to search for a key from the Map when a value is given.
The map stores the data of a state as key and its cities as the list. So it is assumed that there is no duplicate key or value.
Some of the previous answers pointed out the solutions for many:many and one:one relations of key-value in map, but I don't understand how to check for value in the List.
Do I iterate over the whole map and for each key get the list and search in the list? or is there any other way of doing this?
Please suggest some method. Thank you!
You should make inverted map (city -> state map).