MapToPopulate.put(States.WASHINGTON, "Olympia") MapToPopulate.put(States.UTAH, "Salt Lake City") MapToPopulate.put(States.SOUTH_DAKOTA, "Pierre") MapToPopulate.put(States.OREGON, "Salem") MapToPopulate.put(States.NORTH_DAKOTA, "Bismark") MapToPopulate.put(States.NEW_MEXICO, "Sante Fe") MapToPopulate.put(States.NEVADA, "Carson City") MapToPopulate.put(States.IDAHO, "Boise") MapToPopulate.put(States.COLORADO, "Denver") MapToPopulate.put(States.CALIFORNIA, "Sacramento") MapToPopulate.put(States.ARIZONA, "Phoenix") Private static Map generateStatesMap(Class mapClass) * This method also logs any Map implementations for which null values are * Generate and populate a Map of states to capitals with provided Map type. The method accepts a Class that should be the specific implementation of Map to be generated and populated. The next code listing uses the enum above and populates a map of states to their capital cities. * Parameterized enum constructor accepting a state name. * Enum representing select western states in the United Sates. Null does not necessarily indicate that the map contains no mapping for the key it's also possible that the map explicitly maps the key toĬontainsKey operation may be used to distinguish these two cases.įor the post's examples, I will be using the States enum defined next: Map.containsKey(Object): If this map permits null values, then a return value of Before going into that demonstration, I will first point out that the Javadoc documentation for Map.get(Object) does explicitly warn about the subtle differences between In this blog post, I demonstrate these aspects of Null from a "get" call should consistently match a "false" return from the "containsKey" method. Null on a get call for that same key, then it is likely that the key maps to aĬontainsKey(Object) while at the same time returning " Often this doesn't matter, but if it does, one can use Map.containsKey() to determine if the Map to return its value for the given key, but that value might be a null. Map implementation allows for null values, then it is possible for the A common assumption might be made that a null returned from Map.get(Object) indicates there is no entry with the provided key in the map, but this is not always the case. Map's get(Object) method and to react differently based on whether the value returned is null or not. ![]() ![]() Recipe 11.When using Java's Map implementations, it is sometimes common to invoke the.Recipe 11.17, “Accessing Map Values” shows how to avoid an exception while accessing a map key.In this example, I originally used the values methods to get the values from the map, but this produces a new collection, whereas the valuesIterator method returns a lightweight iterator. When chaining methods like this together, be careful about intermediate results. In the first example, because at least one element in the collection contains the String literal ucky, the exists call returns true. Res2: Iterator = MapLike(Alaska, Illinois, Kentucky)Īnd exists returns true if the function you define returns true for at least one element in the collection. This works because the valuesIterator method returns an Iterator: To test whether a value exists in a map, use the valuesIterator method to search for the value using exists and contains: Scala> if (ntains("FOO")) println("Found foo") else println("No foo") States: = Map(AK -> Alaska, IL -> Illinois, KY -> Kentucky) To test for the existence of a key in a Map, use the contains method: ![]() ![]() You want to test whether a Scala Map contains a given key or value. This is one of the shortest recipes, Recipe 11.21, “How to Test for the Existence of a Key or Value in a Scala Map” Problem This is an excerpt from the Scala Cookbook (partially modified for the internet). show more info on classes/objects in repl.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |