- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapOperation.java
More file actions
Latest commit
130 lines (96 loc) · 2.93 KB
/
Copy pathMapOperation.java
File metadata and controls
130 lines (96 loc) · 2.93 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
importjava.nio.file.CopyOption;
importjava.util.Collection;
importjava.util.Comparator;
importjava.util.HashMap;
importjava.util.Hashtable;
importjava.util.Iterator;
importjava.util.Map;
importjava.util.Map.Entry;
importjava.util.Set;
importjava.util.TreeMap;
// Auther : Sidheshwari
publicclassMapOperation {
publicstaticvoidmain(String[] args) {
System.out.println("HashMap operations....");
HashMaphm = newHashMap();
hm.put("Siddhi",25);
hm.put("Aadi", 50);
hm.put("Shiva", 30);
hm.put("Dhanush",35);
hm.put("Kiran", 23);
System.out.println(hm);
hm.put("Siddhi", 30);
System.out.println(hm);
System.out.println(hm.remove("Aadi"));
System.out.println(hm);
SetkSet = hm.keySet();
System.out.println(kSet);
Collectionc = hm.values();
System.out.println(c);
SetentrySet = hm.entrySet();
System.out.println(entrySet);
Iteratoritr = entrySet.iterator();
while(itr.hasNext()) {
Map.Entryme = (Entry) itr.next();
System.out.println(me.getKey() + "----" + me.getValue());
if (me.getKey().equals("Siddhi")) {
me.setValue(26);
}
}
System.out.println(hm);
TreeMapOperationtmp = newTreeMapOperation();
tmp.treeMapCreation();
tmp.treeMapCreationWithCustomizedSorting();
// Hashtable Creation
HashTableOperationht = newHashTableOperation();
ht.createHashTable();
}
}
classTreeMapOperation {
publicvoidtreeMapCreation() {
TreeMaptm = newTreeMap();
tm.put(100, "Aaa");
tm.put(101,"AaA");
tm.put(99, "Kiya");
System.out.println("\nNatural Sorting using TreeMap :--->");
System.out.println(tm);
}
publicvoidtreeMapCreationWithCustomizedSorting() {
TreeMaptm = newTreeMap(newMyComparator());
tm.put(100, "Aaa");
tm.put(101,"AaA");
tm.put(99, "Kiya");
System.out.println("\nCustom Sorting using TreeMap :--->");
System.out.println(tm);
}
}
classHashTableOperation{
publicvoidcreateHashTable() {
Hashtableht = newHashtable();
ht.put("Ajju", 101);
ht.put("Kiran", 102);
ht.put("Aiya",103);
ht.put("ajinkya", 109);
ht.put("abc", 105);
// java.lang.NullPointerException
// ht.put("abcd", null)
System.out.println("\nHashTable creation ....");
System.out.println(ht);
}
}
classMyComparatorimplementsComparator{
@Override
publicintcompare(Objecto1, Objecto2) {
Integeri1 = (Integer)o1;
Integeri2 = (Integer)o2;
return -i1.compareTo(i2);
}
}
classMyComparatorFotHashtableimplementsComparator{
@Override
publicintcompare(Objecto1, Objecto2) {
Strings1 = o1.toString();
Strings2 = o2.toString();
return -s1.compareTo(s2);
}
}