Tutorials

Collection Programs


HashMap add item


In this chapter, our task is:

  • We will create a HashMap
  • We will add elements to HashMap
  • We will print the map

Hashmap stores element in key, value pair.

Example: Create and add elements to Hashmap in java


package HashMap;

import java.util.HashMap;

public class SimpleHashMap {

      public static void main(String[] args) {

            HashMap javaRadarMap = new HashMap();

            javaRadarMap.put(1, "Mukesh");
            javaRadarMap.put(2, "Puja");
            javaRadarMap.put(3, "Kavya");
            javaRadarMap.put(4, "Shweta");
            javaRadarMap.put(5, "Rajneesh");
            javaRadarMap.put(6, "Mithilesh");
          
            System.out.println(javaRadarMap);

      }

}

OUTPUT

{1=Mukesh, 2=Puja, 3=Kavya, 4=Shweta, 5=Rajneesh, 6=Mithilesh}

 

Important points to note

  • In map, we use put with dot(.) operator to add element
  • (1, "Mukesh") - Here 1 is key and "Mukesh" is the value.

 



Please Share this page
Views : 32