Repository navigation
Expand file tree
/
Copy pathPracticeExercises.java
More file actions
416 lines (356 loc) · 15.9 KB
/
Copy pathPracticeExercises.java
File metadata and controls
416 lines (356 loc) · 15.9 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import java.util.*;
public class PracticeExercises {
public static void main(String[] args) {
System.out.println("Array List Exercises: ");
// 15
System.out.println("1. Write a Java program to join two array lists:");
List<String> a1 = new ArrayList<>();
List<String> a2 = new ArrayList<>();
a1.add("lalala");
a1.add("lololo");
a2.add("lelele");
a2.add("lululu");
System.out.println("Array list 1: " + a1);
System.out.println("Array list 2: " + a2);
for (String s : a2) {
a1.add(s);
}
System.out.println(a1 + "\n");
// 21
System.out.println("2. Write a Java program to replace the second element of an ArrayList with the specified element:");
System.out.println(a1);
for (int i = 0; i < a1.size(); i++) {
if (i == 1) {
a1.set(i, "okay");
}
}
System.out.println(a1 + "\n");
// 13
System.out.println("3. Write a Java program to compare two array lists:");
List<String> l1 = new ArrayList<>();
List<String> l2 = new ArrayList<>();
l1.add("lalala");
l1.add("lololo");
l1.add("lelele");
l2.add("lilili");
l2.add("lululu");
l2.add("lelele");
System.out.println("array list 1: " + l1);
System.out.println("array list 2: " + l2);
if (l1.equals(l2)) {
System.out.println("The two array lists are the same.");
} else {
System.out.println("The two array lists are not the same.");
}
System.out.println("\n");
// 17
System.out.println("4. Write a Java program to empty an array list: ");
System.out.println("before clearing: " + l1);
l1.clear();
System.out.println("after clearing: " + l1 + "\n");
// 18
System.out.println("5. Write a Java program to test whether an array list is empty or not: ");
System.out.println("Array list: " + l1);
if (l1.isEmpty()) {
System.out.println("The array list is empty.");
} else {
System.out.println("The array list is not empty.");
}
System.out.println("\n");
// 22
System.out.println("6. Write a Java program to print all the elements of an ArrayList using the elements' position:");
System.out.println("Array list: " + l2);
for (int i = 0; i < l2.size(); i++) {
System.out.println("Element at position " + i + ": " + l2.get(i));
}
System.out.println("\n");
System.out.println("Linked List Exercises: ");
// 14
System.out.println("1. Write a Java program to remove all elements from a linked list:");
LinkedList<Integer> l6 = new LinkedList<>();
l6.add(1);
l6.add(2);
l6.add(3);
System.out.println("Before removing: " + l6);
l6.clear();
System.out.println("After removing: " + l6 + "\n");
// 18
System.out.println("2. Write a Java program to copy a linked list to another linked list:");
LinkedList<Integer> a = new LinkedList<>();
a.add(1);
a.add(2);
LinkedList<Integer> b = new LinkedList<>();
b.addAll(a);
System.out.println("Linked list 1: " + a);
System.out.println("Linked list 2: " + b + "\n");
// 26
System.out.println("3. Write a Java program to replace an element in a linked list:");
LinkedList<String> l7 = new LinkedList<>();
l7.add("lalala");
l7.add("lololo");
l7.add("lelele");
System.out.println("Before replacing: " + l7);
l7.set(2, "okay");
System.out.println("After replacing: " + l7 + "\n");
// 22
System.out.println("4. Write a Java program to check if a particular element exists in a linked list:");
System.out.println("Linked list: " + l7);
String s = "lololo";
if (l7.contains(s)) {
System.out.println(s + " exists in the list");
} else {
System.out.println(s + " does not exist");
}
System.out.println("\n");
// 19
System.out.println("5. Write a Java program to remove and return the first element of a linked list:");
System.out.println("Before removing: " + l7);
String first = l7.removeFirst();
System.out.println("After removing: " + l7);
System.out.println("Removed element: " + first + "\n");
// 23
System.out.println("6. Write a Java program to convert a linked list to an array list:");
System.out.println("Linked list: " + l7);
List<String> l8 = new ArrayList<>(l7);
System.out.println("Array list: " + l8);
System.out.println("\n");
System.out.println("Hash Set Exercises: ");
// 7
System.out.println("1. Write a Java program to convert a hash set to an array:");
HashSet<String> h1 = new HashSet<>();
h1.add("lalala");
h1.add("lololo");
h1.add("lelele");
System.out.println("Hash set: " + h1);
String[] arr = new String[h1.size()];
h1.toArray(arr);
System.out.println("Array: " + Arrays.toString(arr) + "\n");
// 8
System.out.println("2. Write a Java program to convert a hash set to a tree set:");
HashSet<Integer> h2 = new HashSet<>();
h2.add(1);
h2.add(2);
h2.add(10);
System.out.println("Hash set: " + h2);
TreeSet<Integer> t1 = new TreeSet<>(h2);
System.out.println("Tree set: " + t1 + "\n");
// 9
System.out.println("3. Write a Java program to find numbers less than 7 in a tree set.");
System.out.println("Tree set: " + t1);
System.out.println("Numbers less than 7: ");
for (int i : t1) {
if (i < 7) {
System.out.print(i + " ");
}
}
System.out.println("\n");
// 10
System.out.println("\n4. Write a Java program to compare two hash set:");
HashSet<String> h3 = new HashSet<>();
h3.add("lalala");
h3.add("lololo");
System.out.println("Hash set 1: " + h3);
HashSet<String> h4 = new HashSet<>();
h4.add("lalala");
h4.add("lelele");
System.out.println("Hash set 2: " + h4);
if (h3.equals(h4)) {
System.out.println("The two hash sets are the same.");
} else {
System.out.println("The two hash sets are not the same.");
}
System.out.println("\n");
// 11
System.out.println("5. Write a Java program to compare two sets and retain elements that are the same:");
System.out.println("Hash set 1: " + h3);
System.out.println("Hash set 2: " + h4);
h3.retainAll(h4);
System.out.println("Retained elements: " + h3 + "\n");
// 12
System.out.println("6. Write a Java program to remove all elements from a hash set.");
System.out.println("Hash set: " + h2);
h2.clear();
System.out.println("After removing: " + h2 + "\n");
System.out.println("Tree Set Exercises: ");
// 10
System.out.println("1. Write a Java program to get the element in a tree set which is greater than or equal to the given element: ");
t1.add(100);
System.out.println("Tree set: " + t1);
int n = 2;
for (int i : t1) {
if (i >= n) {
System.out.println("Element greater than or equal to " + n + " = " + i);
}
}
System.out.println("\n");
// 11
System.out.println("2. Write a Java program to get the element in a tree set less than or equal to the given element.");
System.out.println("Tree set: " + t1);
int n1 = 5;
for (int i : t1) {
if (i <= n1) {
System.out.println("Element greater than or equal to " + n1 + " = " + i);
}
}
System.out.println("\n");
// 12
System.out.println("3. Write a Java program to get the element in a tree set strictly greater than or equal to the given element.");
System.out.println("Tree set: " + t1);
int n2 = 2;
for (int i : t1) {
if (i > n2) {
System.out.println("Element greater than " + n2 + " = " + i);
}
}
// 14
System.out.println("4. Write a Java program to retrieve and remove the first element of a tree set:");
System.out.println("Tree set: " + t1);
int firstElement = t1.pollFirst();
System.out.println("First element: " + firstElement);
System.out.println("After removing: " + t1 + "\n");
// 15
System.out.println("5. Write a Java program to retrieve and remove the last element of a tree set:");
System.out.println("Tree set: " + t1);
int lastElement = t1.pollLast();
System.out.println("Last element: " + lastElement);
System.out.println("After removing: " + t1 + "\n");
// 15
System.out.println("6. Write a Java program to remove a given element from a tree set:");
System.out.println("Tree set: " + t1);
int n3 = 10;
t1.remove(n3);
System.out.println("After removing " + n3 + ": " + t1 + "\n");
System.out.println("Priority Queue Exercises: ");
// 7
System.out.println("1. Write a Java program to compare two priority queues: ");
PriorityQueue<String> p1 = new PriorityQueue<>();
p1.add("lalala");
p1.add("lololo");
System.out.println("Priority queue 1: " + p1);
PriorityQueue<String> p2 = new PriorityQueue<>();
p2.add("lalala");
p2.add("lelele");
System.out.println("Priority queue 2: " + p2);
if (p1.equals(p2)) {
System.out.println("The two priority queues are the same.");
} else {
System.out.println("The two priority queues are not the same.");
}
System.out.println("\n");
// 8
System.out.println("2. Write a Java program to retrieve the first element of the priority queue:");
System.out.println("Priority queue: " + p1);
String firstElement1 = p1.peek();
System.out.println("First element: " + firstElement1 + "\n");
// 9
System.out.println("3. Write a Java program to retrieve and remove the first element:");
System.out.println("Priority queue: " + p1);
String firstElement2 = p1.poll();
System.out.println("First element: " + firstElement2);
System.out.println("After removing: " + p1 + "\n");
// 10
System.out.println("4. Write a Java program to convert a priority queue to an array containing all its elements:");
System.out.println("Priority queue: " + p2);
String[] arr1 = new String[p2.size()];
p2.toArray(arr1);
System.out.println("Array: " + Arrays.toString(arr1) + "\n");
// 11
System.out.println("5. Write a Java program to convert a Priority Queue element to string representations:");
System.out.println("Priority queue: " + p2);
for (String s1 : p2) {
System.out.println(s1);
}
System.out.println("\n");
// 12
System.out.println("6. Write a Java program to change priorityQueue to maximum priority queue:");
PriorityQueue<Integer> p3 = new PriorityQueue<>();
p3.add(1);
p3.add(2);
p3.add(3);
p3.add(4);
System.out.println("Priority Queue: " + p3);
PriorityQueue<Integer> p4 = new PriorityQueue<>(Collections.reverseOrder());
p4.addAll(p3);
System.out.println("Maximum Priority Queue: " + p4 + "\n");
System.out.println("Hash Map Exercises: ");
// 7
System.out.println("1. Write a Java program to test if a map contains a mapping for the specified key:");
HashMap<Integer, String> m1 = new HashMap<>();
m1.put(1 , "lalala");
m1.put(2 , "lololo");
System.out.println("Map: " + m1);
int key = 2;
if (m1.containsKey(key)) {
System.out.println("contains the key " + key);
} else {
System.out.println("does not contain the key " + key);
}
// 8
System.out.println("2. Write a Java program to test if a map contains a mapping for the specified value:");
System.out.println("Map: " + m1);
String value = "lolole";
if (m1.containsValue(value)) {
System.out.println("contains the value " + value);
} else {
System.out.println("does not contain the value " + value);
}
// 9
System.out.println("3. Write a Java program to create a set view of the mappings contained in a map:");
System.out.println("Map: " + m1);
Set set = m1.entrySet();
System.out.println("Set view: " + set + "\n");
// 10
System.out.println("4. Write a Java program to get the value of a specified key in a map:");
System.out.println("Map: " + m1);
int key1 = 1;
System.out.println("Value of key " + key1 + " = " + m1.get(key1) + "\n");
// 11
System.out.println(("5. Write a Java program to get a set view of the keys contained in this map:"));
System.out.println("Map: " + m1);
Set<Integer> keys = m1.keySet();
System.out.println("Set view of keys: " + keys + "\n");
// 12
System.out.println("6. Write a Java program to get a collection view of the values contained in this map:");
System.out.println("Map: " + m1);
Collection<String> values = m1.values();
System.out.println("Collection view of values: " + values + "\n");
System.out.println("Tree Map Exercises: ");
// 15
System.out.println("1. Write a Java program to get the least key strictly greater than the given key. Return null if there is no such key:");
TreeMap<Integer, String> t2 = new TreeMap<>();
t2.put(1, "lalala");
t2.put(2, "lololo");
t2.put(3, "lelele");
System.out.println("Tree map: " + t2);
int key2 = 2;
System.out.println("Least key strictly greater than " + key2 + " = " + t2.higherKey(key2) + "\n");
//18
System.out.println("2. Write a Java program to get a NavigableSet view of keys in a map:");
System.out.println("Tree map: " + t2);
NavigableSet<Integer> nks = t2.navigableKeySet();
System.out.println("Navigable set view of keys: " + nks + "\n");
//19
System.out.println("3. Write a Java program to remove and get a key-value mapping associated with the least key in a map:");
System.out.println("Tree map: " + t2);
Map.Entry<Integer, String> e = t2.pollFirstEntry();
System.out.println("Removed key-value mapping: " + e);
System.out.println("After removing: " + t2 + "\n");
//20
System.out.println("4. Write a Java program to remove and get a key-value mapping associated with the greatest key in this map:");
System.out.println("Tree map: " + t2);
Map.Entry<Integer, String> e1 = t2.pollLastEntry();
System.out.println("Removed key-value mapping: " + e1);
System.out.println("After removing: " + t2 + "\n");
//21
System.out.println("5. Write a Java program to get the portion of a map whose keys range from a given key (inclusive) to another key (exclusive).");
t2.put(7, "lalala");
t2.put(6, "lelele");
t2.put(5, "lilili");
System.out.println("Tree map: " + t2);
System.out.println("Portion of map: " + t2.subMap(5, 8) + "\n");
//22
System.out.println("6. Write a Java program to get the portion of a map whose keys range from a given key to another key:");
System.out.println("Tree map: " + t2);
System.out.println("Portion of map: " + t2.subMap(5, true, 8, true) + "\n");
}
}