Top 10 Examples of HashMap in java : HashMap tutorial

How HashMap works in java:-

HashMap is implementation of Map interface in java. HashMap is popular collection framework used in every day life of java developer.
In most of the backend programming, HashMap is used to store as temparary cache for either request scope or session scope.HashMap is combination of key and values. key and values should be either String,primitives or any custom object. The key stored in the HashMap is based on the hashCode computed for key'object Map is implemented by HashMap in utils package

Important points to remember for HashMap:-
1. Order of keys stored in HashMap is based on hashing. Eventhough order is not fixed.
2.HashMap allows null keys and null values
3.HashMap is not synchronized meanning is not thread safety
4. Hashmap is fail fast meaning, if we are modifying/adding/remove any object while iteration of HashMap, it throws ConcurrentModificationException

keys stored in order of calculating the Hashing mechansizm .

1. Create HashMap Object using new in java:-
HashMap provides default constructor as well as other constructor with integer number
HasMap map=new HashMap() or
HasMap map=new HashMap(10)
The above one of the two lines create HashMap with emptry constructor or construtctor with integer. This number represetns initial capacity

2. Adding Objects to HashMap.
HashMap provides put method to store the key and values in the map.
HashMap map=new HashMap(10);
map.put("11", "oneone");
map.put("21", "twoone");
map.put("31", "threeone");
map.put("41", "fourone");

3. Itertion of all values in HashMap:-
HashMap provides values() method to iterate all the values
Collection collection=map.values();
 for(String str:collection){
  System.out.print(" "+str);
values() return the colleciton object which we can used in for each loop to return the each object
out put is
twoone fourone threeone oneone

4.Find out the size of hashmap:-

size() method is used to find out the size of the hashmap
return the size as 4 for the above map.

5. Check if key object available in HashMap:-

To check if specific key exist in HashMap, we have to use containsKey(Object obj) of HashMap. if the key is found in the map, it returns true other wise it returns false

map.containsKey("11") return true
map.containsKey("134") return false
6. Check if value object available in HashMap:-

To check if specific value exist in HashMap, we have to use containsValue(Object obj) of HashMap. if the value is found in the map, it returns true other wise it returns false

map.containsValue("oneone") return true
map.containsValue("cloudmap") return false
7. How to delete object from HashMap:-
map provides remove method which will removes key and value from the HashMap object.

To delete key object from HashMap, we have to use remove(Object key) method of HashMap and returns the value for that key

remember only key object should be passed to remove method and returns value for that key if removed, otherwise returns null.

String value=map.remove("21");
the above key "21" is found in map,then removes that key and value pair from the map, and returns the value object.

if key is not found, then null is returned.

8. remove all the objects from the HashMap:-

to remove all the key and value pairs from the map, use clear() method of HashMap.

After clear() method is used, if isEmpty() is called , it returns tru as all the objects has been removed.

9. Converting Map keys to Set:-

To convert the HashMap keys to set, we have to use keySet which returns Set of keys
Set keys=map.keySet();
 for(String str:keys){
  System.out.print(" "+str);
10. How to Synchrnoze HashMap in java:-

HashMap is not synchronized. meaning more than one thread modifies the state of an object. But we can mak HashMap as synchronized if

Map synchronizedHashMap = Collections.synchronizeMap(map);

1 comment:

  1. Good examples, on theory part you can also check How HashMap works in Java


Note: only a member of this blog may post a comment.