-
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);
}
}
output
{Dosai=30, Idli=20, Pongal=50}
- 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);
}
}
output
[Dosai, Idli, Pongal]
Top comments (0)