-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapEx.java
More file actions
53 lines (49 loc) · 1.57 KB
/
HashMapEx.java
File metadata and controls
53 lines (49 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package oops;
/**
*
* @author Sunil Shetty
*/
//hashmap stores item in key-value pairs key=index, value=object
import java.util.*;
public class HashMapEx {
public static void main(String[]args)
{
HashMap<String,String> city= new HashMap<String,String>();
//add item using put
city.put("England","London");
city.put("India", "Delhi");
city.put("Germany","Berlin");
city.put("USA","Washington DC");
System.out.println(city);
System.out.println(city.get("India"));
System.out.println(city.remove("Germany"));
String n = city.remove("USA");
System.out.println("item removed "+n);
System.out.println(city);
System.out.println("Size is "+city.size());
//loop
for(String i:city.values())
{
System.out.println(i);
}
//print keys and values
for(String i:city.keySet())
{
System.out.println("Key: "+i+" values: "+city.get(i));
}
HashMap<String,Integer> cars=new HashMap<String,Integer>();
cars.put("Tesla",1);
cars.put("TATA",2);
cars.put("Honda", 3);
cars.put("Maruti", 4);
for(String i:cars.keySet())
{
System.out.println("Key: "+i+" Values: "+cars.get(i));
}
}
}