DEV Community

V I N O T H
V I N O T H

Posted on

1 1 1 1 1

HashMap in java

  1. Java HashMap Example

    import java.util.*;
    public class HashMapExample1{
    public static void main(String args[]){
    HashMap map=new HashMap();//Creating HashMap.
    map.put(1,"Mango"); //Put elements in Map.
    map.put(2,"Apple");
    map.put(3,"Banana");
    map.put(4,"Grapes");

2.Idli, Dosai,Pongal with prices

package framework;

import java.util.HashMap; 

public class map {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap hm = new HashMap();
        hm.put("Idli",20);//entry
        hm.put("Dosai", 30);//entry
        hm.put("Pongal", 50);//entry
        //key--->set
        //Values-->Collection
        System.out.println(hm);
    }

}

Enter fullscreen mode Exit fullscreen mode

output

{Dosai=30, Idli=20, Pongal=50}

Enter fullscreen mode Exit fullscreen mode
  1. Idli,Dosai,Pongal without prices
package framework;

import java.util.HashMap;
import java.util.Set; 

public class map {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HashMap hm = new HashMap();
        hm.put("Idli",20);//entry
        hm.put("Dosai", 30);//entry
        hm.put("pongal", 50);//entry
        //key--->set
        //Values-->Collection
//      System.out.println(hm);
        Set s = hm.keySet(); 
        System.out.println(s);
    }

}

Enter fullscreen mode Exit fullscreen mode

output

[Dosai, Idli, Pongal]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)