0% found this document useful (0 votes)
3 views4 pages

Java Collections Utility Methods PDF

The document outlines various operations for manipulating lists, including sorting, searching, and modifying elements. It also discusses thread-safe collections, unmodifiable collections, and singleton/empty collections, emphasizing their immutable nature. Additionally, it highlights the importance of type safety in collections and the consequences of attempting to modify immutable structures.

Uploaded by

shindeashish6397
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)
3 views4 pages

Java Collections Utility Methods PDF

The document outlines various operations for manipulating lists, including sorting, searching, and modifying elements. It also discusses thread-safe collections, unmodifiable collections, and singleton/empty collections, emphasizing their immutable nature. Additionally, it highlights the importance of type safety in collections and the consequences of attempting to modify immutable structures.

Uploaded by

shindeashish6397
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

list: [5, 3, 8, 1, 3]

1. Sorting & Ordering


● sort(list) → sorts in natural order → [1, 3, 3, 5, 8]

● sort(list, reverseOrder()) → sorts in descending order → [8, 5, 3, 3, 1]

● reverse(list) → reverses current order → [1, 3, 3, 5, 8]

● shuffle(list) → shuffles randomly → [3, 1, 8, 3, 5] (example random)

● rotate(list, 2) → rotates right by 2 → [3, 5, 3, 1, 8]

● swap(list, 0, 4) → swaps first and last → [8, 5, 3, 1, 3]

2. Searching & Index


● binarySearch(list sorted ascending, 3) → index → 2

● indexOfSubList(list, [3,1]) → first occurrence of sublist → 3

● lastIndexOfSubList(list, [3]) → last occurrence → 4

3. Min, Max, Frequency


● min(list) → smallest element → 1

● max(list) → largest element → 8

● min(list, reverseOrder()) → minimum using comparator → 8


● max(list, reverseOrder()) → maximum using comparator → 1

● frequency(list, 3) → count of 3 → 2

● disjoint(list, [10,20]) → no common elements → true

4. Fill, Copy, Replace


● fill(list, 0) → all elements become 0 → [0,0,0,0,0]

● copy(destList, list) → copies current list to another → [0,0,0,0,0] in dest

● replaceAll(list, 0, 9) → replaces all 0s → [9,9,9,9,9]

5. Thread-Safe Collections
● synchronizedList(list) → thread-safe wrapper → [9,9,9,9,9]

● If multiple threads modify it concurrently, it prevents data corruption.

6. Unmodifiable / Read-only Collections


● unmodifiableList(list) → read-only view → [9,9,9,9,9]

● If you try to add, remove, or set elements, it will throw


UnsupportedOperationException.

● unmodifiableSet(set) → read-only set → {9}; modifying throws exception.

● unmodifiableMap(map) → read-only map → {index:9}; modifying throws


exception.
● unmodifiableSortedMap(sortedMap) → read-only sorted map → {index:9};
modifying throws exception.

7. Singleton / Empty / Copies


● singletonList(5) → immutable list → [5]; modifying throws
UnsupportedOperationException.

● singletonMap("k",5) → immutable map → {k=5}; modifying throws exception.

● emptyList() → empty list → []; any modification throws exception.

● nCopies(3,7) → immutable list → [7,7,7]; trying to change any element throws


exception.

8. Miscellaneous Utilities
● addAll(list, 1,2,3) → add multiple elements → [9,9,9,9,9,1,2,3]

● checkedList(list, [Link]) → type-safe list → [9,9,9,9,9,1,2,3];


adding wrong type would throw ClassCastException.

● checkedSet(set, [Link]) → type-safe set → {1,2,3,9}; adding wrong


type throws exception.

● checkedMap(map, [Link], [Link]) → type-safe map →


{index:9}; adding wrong type throws exception.

✅ Key Notes on Immutable / Unmodifiable Collections:

1. Unmodifiable lists/sets/maps are wrappers around original collections; you cannot


modify through the wrapper.
2. Singleton and nCopies lists are fully immutable; any modification attempt
immediately throws UnsupportedOperationException.

3. Checked collections are type-safe wrappers; modifying with a wrong type throws
ClassCastException, but normal valid operations work.

You might also like