The HashMap class:
In many applicationswerequiretwo types of object to be associatedwith each other. Some examples are as follows.
- Staff in alarge company need to be associatedwith internal telephone numbers.
- A network may need to maintain alink betweeneach user's logon id and their current password and other details, such as accessrights and allowable storage space.
- Members of staff in apersonnel system will need to be associated with details such as theirtax details, salaryand home address.
A hash table can store pairs of linked items in key-value pairs.Inthis generic collection, the programmer must specify the type of both the key and the value to instantiate the collection; for example, <String, Integer>.Inthe first example above, the key could be the name of the staff member and the value would be theirtelephone number. The main characteristic of ahash table is that it allows efficient retrieval of the value associated with agiven key.The keys must be distinct:that is, no two keys can be identical. The values need not be distinct - for example, two people could share the same telephone number.
The hash table concept is implemented in Java by means of the java.util class HashMap. This association of keys with values is known as a map - we say it maps keys to values. The HashMap is a particular way of implementing such a map and this should be used in preference to the older Hashtable class.
There are a number of constructors associated with HashMap. For example, we can create a collection mapping strings to strings as follows:
final int INIT_CAPACITY = 100;
HashMap<String,String> table1 = new HashMap<String,String>();
HashMap<String,String> table2 = new HashMap<String,String>
(INIT_CAPACITY);
The HashMap constructor with no arguments sets up an empty HashMap, which is then referenced by table1. The initial capacity is set to a default value. The HashMap capacity is expanded automatically as needed when items are added. The details of how hash tables work are beyond the scope of this course.
The second constructor with one argument is used to specify the initial capacity - in this case 100 'slots' for key-value pairs. This can be efficient in avoiding repeated work by the system in extending the hash table, if you know approximately the required initial capacity. If you are not familiar with how hash tables work, it is best to use the simpler constructor, with a default initial capacity.
Notice the text <String,String>; this indicates that the key part is a string and the value part is also a string.
An example of a HashMap in action is shown below. It declares a HashMap hm that has String keys and values. Three pairs of key-value data are added to hm and the value associated with the key "Darrel" is retrieved and displayed. After this a for statement is used to retrieve each key and its associated value; these are then displayed.
HashMap <String,String> hm = new HashMap<String, String>();
System.out.println("Hello there"); hm.put("Dave", "London"); hm.put("Robert", "Sheffield"); hm.put("Darrel", "Milton Keynes");
System.out.println("Darrel is associated with "+hm.get("Darrel"));
for(String name: hm.keySet())
{
System.out.println
(name+" is associated with the city "+hm.get(name));
}
![2397_Hashmap.png](https://www.expertsmind.com/../CMSImages/2397_Hashmap.png)
Table shows some of the basic methods for data manipulation associated with the HashMap class. V is the value type and K is the key type that the collection has been instantiated for. If no types are specified then they default to Object
Note that the put method updates the value corresponding to an existing key in the table. However, if the key is not already in the table, it creates a new key-value pair.
![99_Hashmap1.png](https://www.expertsmind.com/CMSImages/99_Hashmap1.png)
Table shows that HashMap has a similar set of standard utility methods to those for ArrayList. However, note that the contains method in Table is replaced by two separate methods in Table, which check for either a matching key or a matching value.
The rest of this section outlines the use of such a hash map in a simple example. This application involves mapping user names to the computer that they use - both items are of type String.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The HashMap class homework and assignments? Live The HashMap class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The HashMap class homework help, java assignment help and The HashMap class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours