0% found this document useful (0 votes)
8 views19 pages

Java Arraylisht

Uploaded by

wnightt51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views19 pages

Java Arraylisht

Uploaded by

wnightt51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

1 //Java ArrayList Methods – Scenarios & Practice Programs

2 //Prepared By : Sumathi R
3 //[Link]
4
5
6 // 1. Add few names to an ArrayList and print them using a loop.
7
8 import [Link];
9 import [Link];
10
11 public class ArrayListExample {
12
13 public List<String> addNames() {
14 ArrayList<String> nameList = new ArrayList<>();
15 [Link]("Sumathi");
16 [Link]("Malathi");
17 [Link]("Karthick");
18 [Link]("Rajendiran");
19 [Link]("Tamilselvi");
20
21 for (String name : nameList) {
22 [Link](name);
23 }
24 return nameList;
25 }
26
27 public static void main(String[] args) {
28 ArrayListExample example = new ArrayListExample();
29 [Link]();
30 }
31 }
32
33
34
35 // 2. Check if a specific name exists in the ArrayList.
36
37 import [Link];
38 public class ArrayListCheckExample {
39
40 public void checkIfNameExists(List<String> neededList) {
41 for (String name : neededList) {
42 if ([Link]("Malathi")) {
43 [Link]("\nYes! Malathi exists!!!!");
44 return;
45 }
46 }
47 [Link]("Nope!");
48 }
49
50 public static void main(String[] args) {
51 List<String> sampleNames = [Link](
52 "Sumathi", "Malathi", "Karthick", "Rajendiran", "Tamilselvi"
53 );
54
55 ArrayListCheckExample example = new ArrayListCheckExample();
56 [Link](sampleNames);
57 }
58 }
59
60
61
62 //3. Remove a name from the ArrayList.
63
64 import [Link];
65 import [Link];
66 import [Link];
67
68 public class ArrayListRemoveExample {
69
70 public void deleteElement(List<String> neededList) {
71 if ([Link]("Malathi")) {
72 [Link]("Sumathi");
73 }
74
75 [Link]("New Name List::: ");
76 for (String name : neededList) {
77 [Link](name);
78 }
79 }
80
81 public static void main(String[] args) {
82 List<String> sampleNames = new ArrayList<>([Link](
83 "Sumathi", "Malathi", "Karthick", "Rajendiran", "Tamilselvi"
84 ));
85
86 ArrayListRemoveExample example = new ArrayListRemoveExample();
87 [Link](sampleNames);
88 }
89 }
90
91
92
93 // 4. Replace the 3rd element with another name.
94
95 import [Link];
96 import [Link];
97 import [Link];
98
99 public class ArrayListReplaceExample {
100
101 public void replaceElement(List<String> neededList) {
102 if ([Link]() >= 3) {
103 [Link](2, "Mohith");
104 }
105
106 [Link]("After replacing 3rd element.....");
107 for (String name : neededList) {
108 [Link](name);
109 }
110
111 }
112
113 public static void main(String[] args) {
114 List<String> sampleNames = new ArrayList<>([Link](
115 "Sumathi", "Malathi", "Karthick", "Rajendiran", "Tamilselvi"
116 ));
117
118 ArrayListReplaceExample example = new ArrayListReplaceExample();
119 [Link](sampleNames);
120 }
121 }
122
123
124
125 // 5/6. Sort the ArrayList alphabetically and in reverse order.
126
127 import [Link];
128 import [Link];
129 import [Link];
130 import [Link];
131
132 public class ArrayListSortExample {
133
134 public void sortElement(List<String> inputList) {
135 /// Sort alphabetically
136 [Link](inputList);
137 [Link]("After sorting element alphabetically.....");
138 for (String s : inputList) {
139 [Link](s);
140 }
141
142 /// Sort in reverse order
143 [Link](inputList, [Link]());
144 [Link]("After sorting element in reverse order.....");
145 for (String s : inputList) {
146 [Link](s);
147 }
148 }
149
150 public static void main(String[] args) {
151 List<String> sampleNames = new ArrayList<>([Link](
152 "Sumathi", "Malathi", "Karthick", "Rajendiran", "Tamilselvi"
153 ));
154
155 ArrayListSortExample example = new ArrayListSortExample();
156 [Link](sampleNames);
157 }
158 }
159
160
161
162 // 7. Print only names longer than 5 characters.
163
164 import [Link];
165 import [Link];
166 import [Link];
167
168 public class ArrayListFilterExample {
169
170 public void printingList(List<String> inputString) {
171 for (String sname : inputString) {
172 if ([Link]() > 5) {
173 [Link](sname);
174 }
175 }
176 }
177
178 public static void main(String[] args) {
179 List<String> sampleNames = new ArrayList<>([Link](
180 "Sumathi", "Malathi", "Karthick", "Rajendiran", "Tamilselvi", "Raj"
181 ));
182
183 ArrayListFilterExample example = new ArrayListFilterExample();
184 [Link](sampleNames);
185 }
186 }
187
188
189
190 // 8. Count how many names start with 'S'.
191
192 import [Link];
193 import [Link];
194 import [Link];
195
196 public class ArrayListCountExample {
197
198 public void countingList(List<String> inputList) {
199 int count = 0;
200 for (String s : inputList) {
201 if ([Link]("S")) {
202 count++;
203 }
204 }
205 [Link]("Count is::::: " + count);
206 }
207
208 public static void main(String[] args) {
209 List<String> sampleNames = new ArrayList<>([Link](
210 "Sumathi", "Malathi", "Suresh", "Rajendiran", "Tamilselvi", "Saravanan"
211 ));
212
213 ArrayListCountExample example = new ArrayListCountExample();
214 [Link](sampleNames);
215 }
216 }
217
218
219 // 9. Check if the ArrayList is empty.
220
221 import [Link];
222 import [Link];
223 import [Link];
224
225 public class ArrayListEmptyCheckExample {
226
227 public void checkList(List<String> checkingString) {
228 if ([Link]()) {
229 [Link]("ArrayList is Empty!..");
230 } else {
231 int listSize = [Link]();
232 [Link]("ListSiz[Link] " + listSize);
233 }
234 }
235
236 public static void main(String[] args) {
237 /// Example 1: Non-empty list
238 List<String> sampleNames = new ArrayList<>([Link](
239 "Sumathi", "Malathi", "Karthick"
240 ));
241
242 /// Example 2: Empty list
243 List<String> emptyList = new ArrayList<>();
244
245 ArrayListEmptyCheckExample example = new ArrayListEmptyCheckExample();
246 [Link](sampleNames);
247 [Link](emptyList);
248 }
249 }
250
251
252
253 // 10. Clear the ArrayList and print it.
254 import [Link];
255 import [Link];
256 import [Link];
257
258 public class ArrayListClearExample {
259
260 public void clearList(List<String> inputList) {
261 [Link]();
262 for (String s : inputList) {
263 [Link](s);
264 }
265 }
266
267 public static void main(String[] args) {
268 List<String> sampleNames = new ArrayList<>([Link](
269 "Sumathi", "Malathi", "Karthick", "Rajendiran"
270 ));
271
272 ArrayListClearExample example = new ArrayListClearExample();
273 [Link](sampleNames);
274 }
275 }
276
277
278
279 // 11. Remove all names that contain the letter 'a'.
280 import [Link];
281 import [Link];
282 import [Link];
283
284 public class ArrayListRemoveByLetterExample {
285
286 public void removingParticularElement(List<String> inputString) {
287 List<String> needToDelete = new ArrayList<>();
288 for (String name : inputString) {
289 if ([Link]("a")) {
290 [Link](name);
291 }
292 }
293 [Link]("After removing names that contain the letter 'a'....");
294 [Link](needToDelete);
295 [Link](inputString);
296 }
297
298 public static void main(String[] args) {
299 List<String> sampleNames = new ArrayList<>([Link](
300 "Sumathi", "Malathi", "Karthick", "Rajendiran", "Tamilselvi", "Mohith"
301 ));
302
303 ArrayListRemoveByLetterExample example = new ArrayListRemoveByLetterExample();
304 [Link](sampleNames);
305 }
306 }
307
308
309 // 12. Create two ArrayLists and find common names (intersection).
310 import [Link];
311 import [Link];
312
313 public class ArrayListIntersectionExample {
314
315 public List<List<String>> findIntersectionOfLists() {
316
317 List<String> list1 = new ArrayList<>();
318 List<String> list2 = new ArrayList<>();
319
320 [Link]("Sumathi");
321 [Link]("Malathi");
322 [Link]("Karthik");
323 [Link]("Tamilselvi");
324 [Link]("Rajendiran");
325
326 [Link]("List1 names ::: " + list1);
327
328 [Link]("Muthuveerappan");
329 [Link]("Sathyavani");
330 [Link]("Jawahar");
331 [Link]("Sumathi");
332 [Link]("Mohith");
333
334 [Link]("List2 names ::: " + list2);
335
336 ArrayList<String> commonList = new ArrayList<>();
337
338 for (String name : list1) {
339 if ([Link](name)) {
340 [Link](name);
341 }
342 }
343 [Link]("Common List is :::: " + commonList);
344
345 List<List<String>> bothLists = new ArrayList<>();
346 [Link](list1);
347 [Link](list2);
348
349 return bothLists;
350 }
351
352 public static void main(String[] args) {
353 ArrayListIntersectionExample example = new ArrayListIntersectionExample();
354 [Link]();
355 }
356 }
357
358
359 // 13. Merge two ArrayLists and remove duplicates.
360
361 import [Link];
362 import [Link];
363 import [Link];
364
365 public class ArrayListMergeRemoveDuplicatesExample {
366
367 public void mergingListThenRemovingDuplicates(List<String> list1, List<String> list2)
{
368
369 [Link]("List1 is :::: " + list1);
370 [Link]("List2 is :::: " + list2);
371
372 /// Merging two lists
373 [Link](list2);
374
375 [Link]("After merging two lists ::: " + list1);
376
377 List<String> uniqueList = new ArrayList<>();
378
379 for (String name : list1) {
380 if (![Link](name)) {
381 [Link](name);
382 }
383 }
384
385 [Link]("UniqueList Names::: " + uniqueList);
386 }
387
388 public static void main(String[] args) {
389 List<String> list1 = new ArrayList<>([Link](
390 "Sumathi", "Malathi", "Karthick"
391 ));
392 List<String> list2 = new ArrayList<>([Link](
393 "Rajendiran", "Malathi", "Mohith"
394 ));
395
396 ArrayListMergeRemoveDuplicatesExample example = new
ArrayListMergeRemoveDuplicatesExample();
397 [Link](list1, list2);
398 }
399 }
400
401
402 // 14. Print names that start and end with the same letter.
403
404 import [Link];
405 import [Link];
406
407 public class ArrayListSameFirstLastExample {
408
409 public List<String> printingList() {
410
411 List<String> nameList = new ArrayList<>();
412 [Link]("Sumathi");
413 [Link]("BhB");
414 [Link]("KarishmaK");
415 [Link]("Malathi");
416 [Link]("AmalA");
417
418 [Link]("Original List: " + nameList);
419
420 List<String> newList = new ArrayList<>();
421
422 for (String name : nameList) {
423 if (name == null || [Link]()) {
424 continue;
425 }
426 char first = [Link](0);
427 char last = [Link]([Link]() - 1);
428
429 if (first == last) {
430 [Link](name);
431 }
432 }
433 [Link]("Printing names that start and end with the same letter :::::
" + newList);
434 return nameList;
435 }
436
437 public static void main(String[] args) {
438 ArrayListSameFirstLastExample example = new ArrayListSameFirstLastExample();
439 [Link]();
440 }
441 }
442
443
444 // 15. Find the longest name in the ArrayList.
445
446 import [Link];
447 import [Link];
448 import [Link];
449
450 public class ArrayListLongestNameExample {
451
452 public String longestNameInTheList(List<String> nameList) {
453 String longestName = null;
454 int max = 0;
455 for (String name : nameList) {
456 int curr = [Link]();
457 if (curr > max) {
458 max = curr;
459 longestName = name;
460 }
461 }
462 [Link]("Longest name is :::: " + longestName);
463 return longestName;
464 }
465
466 public static void main(String[] args) {
467 List<String> sampleNames = new ArrayList<>([Link](
468 "Sumathi", "Malathi", "Karthick", "Rajendiran", "Tamilselvi", "Mohith"
469 ));
470
471 ArrayListLongestNameExample example = new ArrayListLongestNameExample();
472 [Link](sampleNames);
473 }
474 }
475
476
477
478
479
480
481 // [Link] duplicate names using a Set.
482
483 public List<Integer> removingDuplicates() {
484
485 ArrayList<Integer> ar = new ArrayList<Integer>();
486 [Link](8);
487 [Link](10);
488 [Link](15);
489 [Link](12);
490 [Link](8);
491 [Link](6);
492 [Link](10);
493
494 [Link]("Original List :::: " + ar);
495
496 Set<Integer> set = new HashSet<Integer>(ar);
497
498 List<Integer> uniqueList = new ArrayList<Integer>(set);
499
500 [Link]("After removing duplicates :::: " + uniqueList);
501
502 return uniqueList;
503 }
504
505
506
507 // [Link] the ArrayList without using [Link]().
508
509 import [Link];
510 import [Link];
511
512 public class ReverseArrayListExample {
513
514 public List<Integer> reverseList(List<Integer> list) {
515
516 ///with using [Link]()
517 ///[Link](list, [Link]());
518
519 /// without using [Link]()
520 [Link]("Original List: " + list);
521
522 int size = [Link]();
523 for (int i = 0; i < size / 2; i++) {
524 Integer temp = [Link](i);
525 [Link](i, [Link](size - 1 - i));
526 [Link](size - 1 - i, temp);
527 }
528
529 [Link]("Reversed List: " + list);
530 return list;
531 }
532
533 public static void main(String[] args) {
534 ReverseArrayListExample obj = new ReverseArrayListExample();
535
536 List<Integer> numbers = new ArrayList<Integer>();
537 [Link](10);
538 [Link](20);
539 [Link](30);
540 [Link](40);
541 [Link](50);
542
543 [Link](numbers);
544 }
545 }
546
547
548
549
550 // 18. Find duplicates in the ArrayList without using a Set
551
552 import [Link];
553 import [Link];
554
555 public class DuplicateFinder {
556
557 public List<String> findingDuplicatesWithoutSet(List<String> inputList) {
558
559 [Link]("Sumathi");
560 [Link]("Malathi");
561 [Link]("Sumathi");
562 [Link]("Anu");
563 [Link]("Malathi");
564
565 [Link]("Original List is ::: " + inputList);
566
567 List<String> uniqueList = new ArrayList<String>();
568 List<String> duplicatesList = new ArrayList<String>();
569
570 for (String name : inputList) {
571 if ([Link](name)) {
572 if (![Link](name)) {
573 [Link](name);
574 }
575 } else {
576 [Link](name);
577 }
578 }
579
580 [Link]("Duplicate List is :::: " + duplicatesList);
581 return duplicatesList;
582 }
583
584 public static void main(String[] args) {
585 DuplicateFinder obj = new DuplicateFinder();
586 [Link](new ArrayList<String>());
587 }
588 }
589
590
591
592 // 19. Convert the ArrayList into an array and print.
593
594 import [Link];
595 import [Link];
596 import [Link];
597
598 public class ArrayListToArrayExample {
599
600 public void convertingIntoArray() {
601
602 List<Integer> list = new ArrayList<Integer>();
603 [Link](20);
604 [Link](24);
605 [Link](22);
606 [Link](23);
607
608 Integer[] arrayL = [Link](new Integer[0]);
609
610 [Link]([Link](arrayL));
611 }
612
613 public static void main(String[] args) {
614 ArrayListToArrayExample obj = new ArrayListToArrayExample();
615 [Link]();
616 }
617 }
618
619
620
621 // 20. Remove all even numbers from a list
622
623 import [Link];
624 import [Link];
625
626 public class ArrayListEvenRemove {
627
628 public List<Integer> removingEven() {
629 List<Integer> arr1 = new ArrayList<Integer>();
630 [Link](11);
631 [Link](20);
632 [Link](15);
633 [Link](22);
634 [Link](16);
635
636 List<Integer> arr2 = new ArrayList<Integer>();
637
638 for (int no : arr1) {
639 if (no % 2 != 0) {
640 [Link](no);
641 }
642 }
643 [Link]("After removing even numbers from list is ::: " + arr2);
644 return arr2;
645 }
646
647 public static void main(String[] args) {
648 ArrayListEvenRemove obj = new ArrayListEvenRemove();
649 [Link]();
650 }
651 }
652
653
654 // 21. Remove all names with less than 5 letters
655
656 import [Link];
657 import [Link];
658
659 public class RemoveNames {
660
661 public void removingNames() {
662
663 List<String> li = new ArrayList<String>();
664 [Link]("Sumathi");
665 [Link]("Tamilselvi");
666 [Link]("Jawahar");
667 [Link]("Puma");
668 [Link]("Eli");
669 [Link]("Rajesh");
670
671 List<String> li2 = new ArrayList<String>();
672
673 for (String name : li) {
674 if ([Link]() >= 5) {
675 [Link](name);
676 }
677 }
678 [Link]("After removing names with less than 5 characters::: " + li2);
679 }
680
681 public static void main(String[] args) {
682 RemoveNames obj = new RemoveNames();
683 [Link]();
684 }
685 }
686
687
688
689
690 // 22. Remove null or blank values from a list
691
692 import [Link];
693 import [Link];
694
695 public class RemoveBlankValues {
696
697 public static void main(String[] args) {
698 new RemoveBlankValues().removingBlank();
699 }
700 public void removingBlank() {
701
702 List<String> list1 = new ArrayList<String>();
703 [Link]("Sumathi");
704 [Link](null);
705 [Link]("Karthi");
706 [Link]("");
707 [Link](" ");
708 [Link]("Jawahar");
709
710 List<String> list2 = new ArrayList<String>();
711 for (String name : list1) {
712 if (name != null && ![Link]().isEmpty()) {
713 [Link](name);
714 }
715 }
716 [Link]("After removing blank and null values :::: " + list2);
717 }
718 }
719
720
721 // 23. Remove duplicate integers from list
722
723 import [Link];
724 import [Link];
725
726 public class RemoveDuplicatesDemo {
727
728 public static void main(String[] args) {
729 RemoveDuplicatesDemo obj = new RemoveDuplicatesDemo();
730 [Link]();
731 }
732
733 public List<Integer> removingDup() {
734
735 List<Integer> orgList = new ArrayList<Integer>();
736 [Link](10);
737 [Link](20);
738 [Link](12);
739 [Link](10);
740 [Link](12);
741 [Link](30);
742
743 [Link]("Original List is :::: " + orgList);
744
745 List<Integer> uniqueList = new ArrayList<Integer>();
746 List<Integer> dupList = new ArrayList<Integer>();
747
748 for (Integer no : orgList) {
749 if ([Link](no)) {
750 if (![Link](no))
751 [Link](no);
752 } else {
753 [Link](no);
754 }
755 }
756
757 [Link]("Duplicate List is ::: " + dupList);
758 [Link]("After removing duplicates :::: " + uniqueList);
759
760 return uniqueList;
761 }
762 }
763
764
765
766 // 24. Keep only names that start with "S" or "M" (case-insensitive)
767 public List<String> filteringList() {
768
769 List<String> list1 = new ArrayList<String>();
770 [Link]("Sumathi");
771 [Link]("Malathi");
772 [Link]("Sowndraya");
773 [Link]("malavika");
774 [Link]("Karthik");
775 [Link]("Vignesh");
776
777 List<String> list2 = new ArrayList<String>();
778
779 for (String name : list1) {
780 if (name != null && ![Link]()) { /// avoid null pointer
781 String firstLetter = [Link](0, 1).toUpperCase();
782 if ([Link]("S") || [Link]("M")) {
783 [Link](name);
784 }
785 }
786 }
787 [Link]("Names starting with 'S' or 'M' ::: " + list2);
788 return list2;
789 }
790
791
792 // 25. Count the frequency of each name in a list
793
794 import [Link];
795 import [Link];
796 import [Link];
797 import [Link];
798
799 public class FrequencyExample {
800
801 public static void main(String[] args) {
802 FrequencyExample obj = new FrequencyExample();
803 [Link]();
804 }
805
806 public void frequencyChecking() {
807
808 List<String> nameList = new ArrayList<String>();
809 [Link]("Sumathi");
810 [Link]("Mohith");
811 [Link]("Jawahar");
812 [Link]("Sumathi");
813 [Link]("Mohith");
814 [Link]("Rejany");
815 [Link]("Arulselvi");
816 [Link]("Mohith");
817 [Link]("Rejany");
818
819 Map<String, Integer> freqMap = new HashMap<String, Integer>();
820
821 for (String name : nameList) {
822 if ([Link](name)) {
823 [Link](name, [Link](name) + 1);
824 } else {
825 [Link](name, 1);
826 }
827 }
828
829 for ([Link]<String, Integer> entry : [Link]()) {
830 [Link]([Link]() + " occurs " + [Link]() + " times "
);
831 }
832 }
833 }
834
835
836 // 26. Find first repeated integer element
837
838 import [Link];
839 import [Link];
840
841 public class FirstRepeatedIntegerExample {
842
843 public void findingFirstRepeated() {
844
845 List<Integer> elementList = new ArrayList<Integer>();
846 [Link](10);
847 [Link](20);
848 [Link](30);
849 [Link](20);
850 [Link](10);
851
852 List<Integer> uniqueList = new ArrayList<Integer>();
853
854 for (Integer no : elementList) {
855 if ([Link](no)) {
856 [Link]("First repeated integer ::: " + no);
857 return;
858 } else {
859 [Link](no);
860 }
861 }
862 [Link]("No repeated integer found");
863 }
864
865 public static void main(String[] args) {
866 FirstRepeatedIntegerExample example = new FirstRepeatedIntegerExample();
867 [Link]();
868 }
869 }
870
871 // 27. Find first non - repeated character element in a list
872
873 import [Link];
874 import [Link];
875 import [Link];
876 import [Link];
877
878 public class FirstNonRepeatedCharacter {
879
880 public static void main(String[] args) {
881 findingFirstNonRepeated();
882 }
883
884 public static void findingFirstNonRepeated() {
885
886 List<String> li = new ArrayList<String>();
887 [Link]("c");
888 [Link]("h");
889 [Link]("g");
890 [Link]("o");
891 [Link]("o");
892 [Link]("h");
893 [Link]("c");
894
895 [Link]("The original list is ::: " + li);
896
897 Map<String, Integer> mapList = new HashMap<String, Integer>();
898
899 for (String c : li) {
900 if ([Link](c)) {
901 [Link](c, [Link](c) + 1);
902 } else {
903 [Link](c, 1);
904 }
905 }
906
907 for (String ch : li) {
908 if ([Link](ch) == 1) {
909 [Link]("First non-repeated element is ::: " + ch);
910 break; /// stop after finding the first one
911 }
912 }
913 }
914 }
915
916
917 //28. You have two lists of student names — one for students who registered for a
workshop and another for those who actually attended.
918 //Use retainAll() to find the students who both registered and attended.
919
920 import [Link];
921 import [Link];
922
923 public class RetainAllExample {
924
925 public void usingRetainAll() {
926
927 List<String> registeredList = new ArrayList<String>();
928 [Link]("Sumathi");
929 [Link]("Menaka");
930 [Link]("Ishwarya");
931 [Link]("Benny");
932 [Link]("Stephyrose");
933 [Link]("KavithaMalar");
934 [Link]("Senthamarai");
935 [Link]("Yasothai");
936 [Link]("SoniaKumari");
937 [Link]("MerinJose");
938
939 [Link]("Registered List is :::: " + registeredList);
940
941 List<String> attendedList = new ArrayList<String>();
942 [Link]("KavithaMalar");
943 [Link]("Sumathi");
944 [Link]("Yasothai");
945 [Link]("Benny");
946
947 [Link]("Attended List is :::: " + attendedList);
948
949 [Link](attendedList);
950
951 [Link]("The students who both registered and attended :::");
952 for (String name : registeredList) {
953 [Link](name);
954 }
955 }
956
957 public static void main(String[] args) {
958 RetainAllExample example = new RetainAllExample();
959 [Link]();
960 }
961 }
962
963
964
965
966 //29. You are managing a list of students who registered for a session and another list
of students who attended the session.
967 //Write a Java program to find the names of students who registered but did not attend
the session.//use removeAll()
968
969 import [Link];
970 import [Link];
971
972 public class RemoveAllExample {
973
974 public static void main(String[] args) {
975 RemoveAllExample example = new RemoveAllExample();
976 [Link]();
977 }
978
979 public void usingRemoveAll() {
980
981 List<String> registeredList = new ArrayList<String>();
982 [Link]("Sumathi");
983 [Link]("Menaka");
984 [Link]("Ishwarya");
985 [Link]("Benny");
986 [Link]("Stephyrose");
987 [Link]("KavithaMalar");
988 [Link]("Senthamarai");
989 [Link]("Yasothai");
990 [Link]("SoniaKumari");
991 [Link]("MerinJose");
992
993 [Link]("Registered List is :::: " + registeredList);
994
995 List<String> attendedList = new ArrayList<String>();
996 [Link]("KavithaMalar");
997 [Link]("Sumathi");
998 [Link]("Yasothai");
999 [Link]("Benny");
1000
1001 [Link]("Attended List is :::: " + attendedList);
1002
1003 [Link](attendedList);
1004
1005 [Link]("The names of students who registered but did not attend the
session...");
1006
1007 for (String name : registeredList) {
1008 [Link](name);
1009 }
1010 }
1011 }
1012
1013
1014 //[Link] Two Department Employee Lists.
1015
1016 import [Link];
1017 import [Link];
1018
1019 class Employee {
1020 private String name;
1021 private String department;
1022 private int id;
1023
1024 public Employee(String name, String department, int id) {
1025 [Link] = name;
1026 [Link] = department;
1027 [Link] = id;
1028 }
1029
1030 @Override
1031 public String toString() {
1032 return name + " (" + department + ", " + id + ")";
1033 }
1034 }
1035
1036 public class MergeEmployeeLists {
1037
1038 public void mergingEmp() {
1039 List<Employee> teamA = new ArrayList<Employee>();
1040 [Link](new Employee("Sumathi", "Gems", 1581));
1041 [Link](new Employee("Rejany", "Gems", 1234));
1042 [Link](new Employee("Arul", "Gems", 1641));
1043 [Link](new Employee("Geethanjali", "Gems", 1598));
1044
1045 List<Employee> teamB = new ArrayList<Employee>();
1046 [Link](new Employee("Krithika", "BFS", 1582));
1047 [Link](new Employee("Jagadhaaranya", "BFS", 1584));
1048 [Link](new Employee("Malini", "BFS", 1345));
1049 [Link](new Employee("Padmapriya", "BFS", 2345));
1050
1051 List<Employee> allEmployees = new ArrayList<Employee>();
1052 [Link](teamA);
1053 [Link](teamB);
1054
1055 [Link]("Two Department Employee Lists ::: " + allEmployees);
1056 }
1057
1058 public static void main(String[] args) {
1059 MergeEmployeeLists obj = new MergeEmployeeLists();
1060 [Link]();
1061 }
1062 }
1063
1064
1065 // 31. Find the First Occurrence of an Item in a Shopping List
1066
1067 import [Link];
1068 import [Link];
1069
1070 public class ShoppingListSearch {
1071
1072 public static void main(String[] args) {
1073 findingFirstOccurance();
1074 }
1075
1076 public static void findingFirstOccurance() {
1077
1078 List<String> shoppingList = new ArrayList<String>();
1079 [Link]("Maggi");
1080 [Link]("Maida");
1081 [Link]("Veggies");
1082 [Link]("Pulses");
1083 [Link]("Maida");
1084 [Link]("Nuts");
1085
1086 /// String searchItem = "Veggies"; // Hardcoded example
1087 String searchItem = "Veggies";
1088 [Link]("The search item is ::: " + searchItem);
1089
1090 int occurance = [Link](searchItem);
1091 int last = [Link](searchItem);
1092
1093 if (occurance != -1) {
1094 [Link]("The first occurrence of the search item \"" + searchItem
+ "\" occurs at index " + occurance);
1095 [Link]("The last occurrence of the search item is at index " +
last);
1096 } else {
1097 [Link]("Your search item is not in your shopping list");
1098 }
1099 String grocery = [Link](2);
1100 [Link]("The grocery at index 2 is: " + grocery);
1101 }
1102 }
1103
1104
1105 //32. You are maintaining a List<String> for your daily to-do tasks. You want to extract
only the tasks planned
1106 //between 11:00 AM and 2:00 PM, which are stored at positions 3 to 6 (inclusive of 3,
exclusive of 7).
1107
1108 import [Link];
1109 import [Link];
1110 class Task {
1111 String name;
1112 int time; /// hour in 24h format (e.g., 7 = 7AM, 12 = Noon)
1113
1114 public Task(String name, int time) {
1115 [Link] = name;
1116 [Link] = time;
1117 }
1118 @Override
1119 public String toString() {
1120 return name + "(" + time + "h)";
1121 }
1122 }
1123 public class TaskFilterExample {
1124
1125 public List<Task> filteringRange() {
1126
1127 List<Task> dailyTasks = new ArrayList<>();
1128 [Link](new Task("Wakeup", 7));
1129 [Link](new Task("Walking", 8));
1130 [Link](new Task("Bathing", 9));
1131 [Link](new Task("Eating", 10));
1132 [Link](new Task("Studying", 11));
1133 [Link](new Task("Writing", 12));
1134
1135 List<Task> filtered = new ArrayList<>();
1136 for (Task t : dailyTasks) {
1137 if ([Link] >= 8 && [Link] <= 11) {
1138 [Link](t);
1139 }
1140 }
1141 [Link]("Filtered to do list from 8 to 11 ::: " + filtered);
1142 return filtered;
1143 }
1144 /// Using subList to get tasks between indexes 3 (inclusive) and 6 (exclusive)
1145 public void usingSublist() {
1146 List<String> toDoList = new ArrayList<>();
1147 [Link]("Walking"); ///0
1148 [Link]("Bathing"); ///1
1149 [Link]("Eating"); ///2
1150 [Link]("Studying"); ///3
1151 [Link]("Sleeping"); ///4
1152 [Link]("Writing"); ///5
1153 [Link]("Speaking"); ///6
1154 [Link]("Chatting"); ///7
1155 [Link]("Painting"); ///8
1156
1157 List<String> filteredList = [Link](3, 6); /// from index 3 to 5
1158 [Link]("Filtered to do list from index 3 to 5 ::: " + filteredList);
1159 }
1160 public static void main(String[] args) {
1161 TaskFilterExample example = new TaskFilterExample();
1162 [Link]();
1163 [Link]();
1164 }
1165 }
1166
1167
1168 // [Link] cart before discount to restore if needed.
1169
1170 import [Link];
1171
1172 public class CartBackupExample {
1173
1174 private void backupCart() {
1175
1176 ArrayList<String> cartList = new ArrayList<String>();
1177 [Link]("Shampoo");
1178 [Link]("Soap");
1179 [Link]("Towel");
1180 [Link]("Brush");
1181 [Link]("TennisBall");
1182
1183 [Link]("The original cartlist is ::: " + cartList);
1184
1185 /// Clone the cartList to create a backup
1186 ArrayList<String> backupCart = (ArrayList<String>) [Link]();
1187 [Link]("Backup cart is ::: " + backupCart);
1188
1189 /// Remove one item from original list
1190 [Link]("Brush");
1191 [Link]("After removing one item from org list is ::: " + cartList);
1192
1193 /// Backup remains unchanged
1194 [Link]("Backup cart is ::: " + backupCart);
1195 }
1196
1197 public static void main(String[] args) {
1198 CartBackupExample example = new CartBackupExample();
1199 [Link]();
1200 }
1201 }
1202
1203
1204 //34. Write a Java program that reads product IDs from a file (or generates them) and
stores them in an ArrayList.
1205 //Use ensureCapacity() to prepare the list for all elements before insertion, so
resizing happens only once.
1206
1207 import [Link];
1208
1209 public class EnsureCapacityExample {
1210
1211 public void usingEnsureCapacity() {
1212 ArrayList<String> productList = new ArrayList<String>();
1213 [Link](1000); /// Pre-allocate capacity for 1000 elements
1214
1215 for (int i = 1; i <= 100; i++) {
1216 [Link]("Product-" + i);
1217 }
1218 [Link](productList);
1219 }
1220
1221 public static void main(String[] args) {
1222 EnsureCapacityExample example = new EnsureCapacityExample();
1223 [Link]();
1224 }
1225 }
1226
1227
1228
1229 // [Link] methods of arraylist used in this file.
1230
1231 import [Link];
1232 import [Link];
1233
1234 public class ArrayListMethodsDemo {
1235
1236 public List<String> showMethodsofArrayList() {
1237
1238 List<String> methodsList = new ArrayList<String>();
1239 List<String> methodsList1 = new ArrayList<String>();
1240
1241 [Link]("ArrayList Specific methods :: ");
1242 [Link]("add");
1243 [Link]("contains");
1244 [Link]("remove");
1245 [Link]("get");
1246 [Link]("set");
1247 [Link]("size");
1248 [Link]("sort");
1249 [Link]("clear");
1250 [Link]("remove");
1251 [Link]("isEmpty /isBlank");
1252 [Link]("toArray");
1253 [Link]("retainAll");
1254 [Link]("removeAll");
1255 [Link]("addAll");
1256 [Link]("subList");
1257 [Link]("indexOf");
1258 [Link]("lastIndexOf");
1259 [Link]("clone");
1260
1261 for (String method : methodsList) {
1262 [Link](method);
1263 }
1264
1265 [Link]("\nString specific methods used in list elements :::");
1266 [Link]("toString");
1267 [Link]("charAt");
1268 [Link]("startsWith");
1269 [Link]("endsWith");
1270 [Link]("equals");
1271 [Link]("equalsIgnoreCase");
1272 [Link]("trim");
1273
1274 for (String method : methodsList1) {
1275 [Link](method);
1276 }
1277
1278 return methodsList;
1279 }
1280
1281 public static void main(String[] args) {
1282 ArrayListMethodsDemo demo = new ArrayListMethodsDemo();
1283 [Link]();
1284 }
1285 }
1286
1287
1288 //Prepared By - Sumathi R // [Link]
1289

You might also like