-
Notifications
You must be signed in to change notification settings - Fork 4
/
LinkedHashMap_Learn.java
221 lines (171 loc) · 6.94 KB
/
LinkedHashMap_Learn.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Set;
/*
* A map stores key-value pairs. Both key and values are objects.
* Using a key you can find its value. Keys must be unique, but
* the values may contain duplicates.
*/
public class LinkedHashMap_Learn {
public static void main(String[] args) {
/*
* The 'Map' interface maps unique keys to values. A key is an object that can
* be used to retrieve its corresponding value. The 'AbstractMap' class
* implements most of the 'Map' interface and serves as a superclass for all
* concrete map implementations.
*
* The 'LinkedHashMap' class extends the 'HashMap' class. It maintains a linked
* list of the entries in the map, in the order in which they were inserted.
* This allows insertion-order iteration over the map, i.e. the order in which
* elements are added to a hash map is the order in which they are accessed by
* an iterator.
*/
Map<Integer, String> studentMap = new LinkedHashMap<>();
/*
* Adding elements to LinkedHashMap
*
* V put(K k, V v) : Declared in the 'Map' interface. Puts an entry in the
* invoking map, overwriting any previous value associated with the key. 'k' is
* the key and 'v' is the value. Returns null if the key did not already exist.
* Otherwise, the previous value linked to the key is returned.
*
* void putAll(Map m) : Declared in the 'Map' interface. Puts all the entries
* from m into the map.
*
* V putIfAbsent(K k, V v) : Declared in the 'Map' interface. Inserts the
* key-value pair into the invoking map if this entry is not already present or
* if the existing value for key k is null. Returns the old value or null value
* if key k was not already present in the map.
*/
studentMap.put(1, "Naman");
studentMap.put(2, "Vivek");
studentMap.put(3, "Payal");
System.out.println("studentMap = " + studentMap);
// studentMap = {1=Naman, 2=Vivek, 3=Payal}
Map<Integer, String> newStudents = new LinkedHashMap<>();
newStudents.put(4, "Neha");
newStudents.put(5, "Anupam");
studentMap.putAll(newStudents);
System.out.println("studentMap = " + studentMap);
// studentMap = {1=Naman, 2=Vivek, 3=Payal, 4=Neha, 5=Anupam}
String existingStudent = studentMap.putIfAbsent(5, "Rohan");
System.out.println("existingStudent = " + existingStudent);
// existingStudent = Anupam
existingStudent = studentMap.putIfAbsent(6, "Sumit");
// studentMap = {1=Naman, 2=Vivek, 3=Payal, 4=Neha, 5=Anupam, 6=Sumit}
System.out.println("existingStudent = " + existingStudent);
// existingStudent = null
/*
* Removing elements from LinkedHashMap
*
* V remove(K k) : Declared in the 'Map' interface. Removes the entry whose key
* equals k.
*
* boolean remove(K k, V v) : Declared in the 'Map' interface. Removes that
* entry from the invoking map if key equals k and value equals v and returns
* true. Otherwise false is returned.
*/
// studentMap = {1=Naman, 2=Vivek, 3=Payal, 4=Neha, 5=Anupam, 6=Sumit}
studentMap.remove(4);
// studentMap = {1=Naman, 2=Vivek, 3=Payal, 5=Anupam, 6=Sumit}
studentMap.remove(6, "Sumit");
// studentMap = {1=Naman, 2=Vivek, 3=Payal, 5=Anupam}
/*
* Get values from LinkedHashMap
*
* V get(K k) : Declared in the 'Map' interface. Returns the value associated
* with key k. Returns null if the key is not found.
*/
// studentMap = {1=Naman, 2=Vivek, 3=Payal, 5=Anupam}
String studentName = studentMap.get(2);
System.out.println("studentName = " + studentName);
// studentName = Vivek
studentName = studentMap.get(4);
System.out.println("studentName = " + studentName);
// studentName = null
/*
* Check if LinkedHashMap contains a key / value
*
* boolean containsKey(K k) : Declared in the 'Map' interface. Returns true if
* key k is present in the invoking map. Otherwise, returns false.
*
* boolean containsValue(V v) : Declared in the 'Map' interface. Returns true if
* value v is present in the invoking map. Otherwise, returns false.
*/
// studentMap = {1=Naman, 2=Vivek, 3=Payal, 5=Anupam}
if (studentMap.containsKey(3))
System.out.println("Key found");
else
System.out.println("Key not found");
if (studentMap.containsValue("Pankaj"))
System.out.println("Value found");
else
System.out.println("Value not found");
/*
* Replace key / value in LinkedHashMap
*
* boolean replace(K k, V oldV, V newV) : Declared in the 'Map' interface. If
* key-value pair specified by k and oldV is present in the invoking map, the
* value is replaced by newV and true is returned. Otherwise false is returned.
*
* V replace(K k, V v) : Declared in the 'Map' interface. If key k is present in
* the invoking map, its corresponding value is set to v and previous value is
* returned. Otherwise null is returned.
*/
// studentMap = {1=Naman, 2=Vivek, 3=Payal, 5=Anupam}
studentMap.replace(3, "Payal", "Sejal");
// studentMap = {1=Naman, 2=Vivek, 3=Sejal, 5=Anupam}
String oldName = studentMap.replace(3, "Ronit");
System.out.println("oldName = " + oldName); // oldName = Sejal
System.out.println("studentMap = " + studentMap);
// studentMap = {1=Naman, 2=Vivek, 3=Ronit, 5=Anupam}
/*
* Get the count of key-value pairs in the LinkedHashMap
*
* int size() : Declared in the 'Map' interface. Returns the number of key-value
* pairs in the map.
*/
// studentMap = {1=Naman, 2=Vivek, 3=Ronit, 5=Anupam}
int mapSize = studentMap.size();
System.out.println("mapSize = " + mapSize); // mapSize = 4
/*
* Check if LinkedHashMap is empty or not
*
* boolean isEmpty() : Declared in the 'Map' interface. Returns true if the
* invoking map is empty. Otherwise, returns false.
*/
// studentMap = {1=Naman, 2=Vivek, 3=Ronit, 5=Anupam}
if (studentMap.isEmpty())
System.out.println("Map is empty");
else
System.out.println("Map is not empty");
/*
* Iterating over LinkedHashMap entries
*
* Set<Map.Entry<K, V>> entrySet() : Declared in the 'Map' interface. Returns a
* Set that contains the entries in the map. The set contains objects of type
* Map.Entry, i.e. this method provides a set-view of the invoking map.
*/
Set<Map.Entry<Integer, String>> mapSet = studentMap.entrySet();
for (Map.Entry<Integer, String> kv : mapSet) {
System.out.print(kv.getKey() + " -> " + kv.getValue() + " ");
}
// 1 -> Naman 2 -> Vivek 3 -> Ronit 5 -> Anupam
System.out.println();
for (Map.Entry kv : studentMap.entrySet()) {
System.out.print(kv.getKey() + " -> " + kv.getValue() + " ");
}
// 1 -> Naman 2 -> Vivek 3 -> Ronit 5 -> Anupam
System.out.println();
/*
* Remove all key-value pair from the LinkedHashMap
*
* void clear() : Declared in the 'Map' interface. Removes all key-value pairs
* from the invoking map.
*/
// studentMap = {1=Naman, 2=Vivek, 3=Ronit, 5=Anupam}
studentMap.clear();
// studentMap = {}
System.out.println("studentMap = " + studentMap);
}
}