One of my favorite of all the Collections classes of Java is the HashMap. Its easy to use and easy to understand. HashMap is actually a hash table implementation of the Map interface. The HashMap class is equivalent of HashTable only that the HashMap allows null values and keys. Moreover hashmap is unsynchronized compared to HashTable. Since this is a lot faster because it is not synchronized however it would not be thread safe.
In dealing with this class, always remember that the order of elements is changing over time. In other words the placement of element on this pool of objects is dispersed.
Now that we have described the HashMap let’s take a look on a simple example.
Basic Example of HashMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.javatutorialhq.java.examples; import java.util.HashMap; /* * Basic HashMap example * */ public class HashMapBasicExample { public static void main(String[] args) { // initialization of our map HashMap<String, String> student = new HashMap<>(); // adding elements to our hashmap student.put("age", "32"); student.put("gender","male"); student.put("name","Ryan"); student.put("birthday","May 12, 1982"); // getting the contents of our hashmap System.out.println("Name:"+student.get("name")); System.out.println("Name:"+student.get("age")); System.out.println("Name:"+student.get("gender")); System.out.println("Name:"+student.get("birthday")); } } |
The above example will yield on the following result
1 2 3 4 |
Name:Ryan Name:32 Name:male Name:May 12, 1982 |
The above example is very simple though the following lessons has been learned:
- How to instantiate a hashmap. Specifying the type of key value pair
- Its also been shown how to add elements on the hashmap using the put method.
- The get() method were used to access the corresponding value of the specified key argument.
Now that we have know the basics of dealing with HashMap, lets try a more concrete example
HashMap of Objects
This example deals with the use of hashmap in storing of objects. There are a lot of things that happen to this example but to give you an overview here’s what it have.
- Create a Student class which takes a string on its constructor.
- The constructor must able to parse the string into tokens and assign it using the setters and getters of the Student class.
- On the main Class create a init() method that instantiate objects of Students by calling its constructors
- The init method should be able to return a HashMap with id as the key in getting Student values.
- Print all the id number on the hashmap and then the student age with respect to the id number.
With the above requirements, please try to create the implementation and compare below for my solution
Here is the Student Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
package com.javatutorialhq.java.examples.utilities; import java.util.StringTokenizer; public class Student { public Student(String s) { /* * Pattern is id|name|bday|age|course */ // initializer our tokenizer StringTokenizer st = new StringTokenizer(s, "|"); // set the values this.setStudentid(st.nextToken()); this.setName(st.nextToken()); this.setBirthday(st.nextToken()); this.setAge(st.nextToken()); this.setCourse(st.nextToken()); } public Student() { super(); } String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } public String getCourse() { return course; } |