-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAugust01a.java
61 lines (48 loc) · 1.27 KB
/
August01a.java
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
54
55
56
57
58
59
60
61
package August;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class August01a {
public static void main(String[] args) {
HashMap<String ,Integer> People = new HashMap<>();
//adding data
People.put("Ram", 25);
People.put("Rammy", 65);
People.put("Kiran", 39);
People.put("Hari", 45);
People.put("Prem", 29);
//print out
System.out.println(People);
// looping through HashMap
System.out.println(People.values());
System.out.println(People.keySet());
System.out.println("****Next case key*********");
//
// foreach loop
for(String key: People.keySet())
{
System.out.println(key);
}
for(int value: People.values())
{
System.out.println(value);
}
System.out.println("****Next case*********");
for (Map.Entry<String, Integer> abc: People.entrySet())
{
String k = abc.getKey();
Integer o = abc.getValue();
System.out.println(k);
System.out.println(o);
System.out.println("Name :" + k + ", Age: " + o);
}
System.out.println("****Next case*********");
// using while loop
Iterator it = People.entrySet().iterator();
while(it.hasNext())
{
Map.Entry me= (Map.Entry) it.next();
System.out.println("Name :"+me.getKey()+" Age:"+me.getValue());
}
}
}