HashMap Example Java Program
Definition
A Hashmap is a Map data structure. Like a list each item that is stored in a hashmap is stored at a particular index. This index is called a hash and it is generated using a hash function. Hash functions accept the object to be stored as an argument and generate a number that is unique to it.Syntax
HashMapVariable_name = new HashMap();
HashMap Example Program
import java.util.HashMap; public class HashMapExample { public static void main(String a[]){ HashMaphm = new HashMap (); hm.put("1","FIRST"); hm.put("2","SECOND"); hm.put("3","THIRD"); hm.put("4",null); hm.put(null,"FIFTH"); System.out.println("hashmap is "+hm); System.out.println("Value of 1: "+hm.get("1")); System.out.println("Is HashMap empty? "+hm.isEmpty()); hm.remove("2"); System.out.println("After removal process, the hashmap is "+hm); System.out.println("Size of the HashMap: "+hm.size()); } }
Sample Output
Output is: hashmap is {null=FIFTH, 3=THIRD, 2=SECOND, 1=FIRST, 4=null} Value of 1: FIRST Is HashMap empty? false After removal process, the hashmap is {null=FIFTH, 3=THIRD, 1=FIRST, 4=null} Size of the HashMap: 4
Java Collection Programs
- Linked List Example Java Program
- ArrayList Example Java Program
- HashSet Example Java Program
- Clear an Arraylist Example Java Program
- Reversing an ArrayList Example Java Program
- Linked HashSet Example Java Program
- HashMap Example Java Program
- Set Value in ArrayList Example Java Program
- Linked HashMap Example Java Program
- Remove Element from ArrayList Example Java Program
- Identity HashMap Example Java Program
- TreeMap Example Java Program
- TreeSet Example Java Program
- Finding Duplicates in Array Using TreeSet Java Example Program
- HashTable Example Java Program
- EnumSet Example Java Program
- Enum Map Example Java Program
- Wrapper Example Java Program
- Un Modifiable Wrapper Example Java Program
- Iterator Interface Example Java Program
- Sorted Map Interface Example Java Program
- Sorted Set Interface Example Java Program
Read More Articles
- Multiple Inheritance Using Interface Example Java Program
- Single Inheritance Example Java Program
- Multilevel Inheritance Example Java Program
- Hierarchical Inheritance Example Java Program
- Find all Substrings of a given string Example Java Program
- Create Matrix Example Java Program
- Sum Of Three Numbers Example Java Program
- Heap Sort Example Java Program
- Twin Prime Example Java Program
- Compile Time Polymorphism Example Java Program