I have a Map that has Strings for its keys and sets (that contain integers) for its values
Say my keys look like this "apple", "banana", "orange", etc.
The user enters text and I save it as a String variable. How do I search my map for an identical key?
So if the user types in "apple" how do I give that String to a method and have the method search my map for the "apple" key and return the set of Integers associated with it (values)?
Thanks
Use the get method
Set<Integer> values = map.get("apple");
You don't really search a Map, you retrieve values from it thus:
public static void main(String[] args) {
final Map<String, Set<Integer>> myMap = new HashMap<>();
//put new values into map
myMap.put("MyString", new HashSet<Integer>());
//get Set from Map
final Set<Integer> mySet = myMap.get("myString");
}
return the set of Integers associated with it (values)?
A map requires the keys to be different, hence I assume that your map declaration would look like Map<String, List<Integer>> myMap
To check if a key exists in a Map:
myMap.containsKey(key)
e.g.
myMap.containsKey("apple")
To get the values associated with the key:
myMap.get(key)
e.g.
myMap.get("apple")