This suits us perfectly, since we can use the passport number as the key and avoid duplicates. Also we specify the key type (Integer) and the value type (String).įirst, a HashMap key is always unique. The number will act as the key, and the string as the value. Here we create a dictionary that stores elements as "number-string" pairs. HashMap passportsAndNames = new HashMap() So, what do we need to know about HashMap in Java?It is very easy to create: Today we'll study the most common implementation of the Map class: Java HashMap. Anything can serve as keys and values: numbers, strings, or objects of other classes. How is it fundamentally different than the data structures we've considered previously?Ībove all, in the fact that data in a Map is stored as key-value pairs. More commonly, it is called a "dictionary" or "map". It is also known as an "associative array", but this term is used infrequently. This should never be allowed: no two people have the same passport number.Ī new data structure comes to our aid: Map. And this is our solution's most serious shortcoming. Second, nothing stops us from creating two different people with the same passport number. And this will be problematic given this information storage format. First, we may need the ability to search by passport number. For example, you can stuff both into a string, and then create a list of these strings:īut this solution has two drawbacks. package the following task: create a list of 100 people that stores each person's name and passport number. From the main method we put a check if the HashMap contains a key before we retrieve the contents, if the key is not on the database we print a “Not found” message on the console. The HashMap returned by the init() method is having an Integer object as key and String object as values. From the example below, basically we have a method init() which actually do the assignment of values to the HashMap object which are expected to be returned by this method. This java example source code demonstrates the use of containsKey() method of HashMap class. The containsKey(Object key) method returns true if this map contains a mapping for the specified key. The key where which we want check if the HashMap contains it Public boolean containsKey(Object key) Parameter Input : ![]() overrides containsKey in class AbstractMap. ![]() specified by containsKey in interface Map.Important notes for contains(Object key) method: This is essential if need to check first if its available before doing any business logic. Getting back to the original topic, this method basically just a way to check if our key is in our HashMap. If you are interested to learn more about this, please visit the official oracle documentation on Generics. But what if we pass a primitive int as key? Will it give a compilation problem or runtime problem? With the autoboxing in place as of Java 5, this is now possible. With this Generics in place, the object type we have to pass is restricted to Integer as key while for the value is String. Lets do a recap on Generics by taking below declaration of HashMap:įrom the above code snippet, the key would be Integer while the value is String. So it means that we cannot just pass String on containsKey() method if we have declared Integer as an argument. But from our discussion on HashMap we have tackled that the HashMap make use of Generics. Since everything that we are declaring in java is a subclass of Object, any type is allowed to be passed as method argument. ![]() Generally we need to pass an object as method argument to the containsKey() method. Basically this method is used to check if the key that we have is available on the list of keys on our HashMap object. On this document we will be showing a java example on how to use the containsKey(Object key) method of HashMap Class.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |