0% found this document useful (0 votes)
1 views2 pages

Collections Method in Java

The document provides an overview of key methods in the Java Collections class, including examples of how to use each method. Key methods include sort(), reverse(), shuffle(), and others, each serving specific purposes such as sorting lists, reversing elements, and counting occurrences. A summary table is also included for quick reference on the use cases of each method.
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)
1 views2 pages

Collections Method in Java

The document provides an overview of key methods in the Java Collections class, including examples of how to use each method. Key methods include sort(), reverse(), shuffle(), and others, each serving specific purposes such as sorting lists, reversing elements, and counting occurrences. A summary table is also included for quick reference on the use cases of each method.
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

🔰 Collections Class – Key Methods with Java Code

Examples
📦 Import Required:

import [Link].*;

1. ✅ sort() – Sort a list


List<Integer> nums = [Link](4, 1, 5, 2);
[Link](nums);
[Link](nums); // Output: [1, 2, 4, 5]

2. ✅ reverse() – Reverse a list


[Link](nums);
[Link](nums); // Output: [5, 4, 2, 1]

3. ✅ shuffle() – Randomly shuffle elements


[Link](nums);
[Link](nums); // Output: [random order]

4. ✅ swap() – Swap elements at two positions


[Link](nums, 0, 2);
[Link](nums); // Output: depends on the previous order

5. ✅ max() – Get the maximum element


int max = [Link](nums);
[Link]("Max: " + max); // Output: Max: 5

6. ✅ min() – Get the minimum element


int min = [Link](nums);
[Link]("Min: " + min); // Output: Min: 1

7. ✅ frequency() – Count frequency of an element


List<String> names = [Link]("ram", "shyam", "ram", "mohan");
int count = [Link](names, "ram");
[Link]("Frequency of 'ram': " + count); // Output: 2

8. ✅ binarySearch() – Fast search on sorted list


List<Integer> sorted = [Link](1, 2, 3, 4, 5);
int index = [Link](sorted, 3);
[Link]("Index of 3: " + index); // Output: 2

9. ✅ fill() – Replace all elements with a single value


List<Integer> fillList = new ArrayList<>([Link](1, 2, 3, 4));
[Link](fillList, 0);
[Link](fillList); // Output: [0, 0, 0, 0]

10. ✅ copy() – Copy contents from one list to another


List<String> src = [Link]("A", "B", "C");
List<String> dest = new ArrayList<>([Link]("X", "Y", "Z"));
[Link](dest, src);
[Link](dest); // Output: [A, B, C]

📌 Note: Destination list must be at least same size.

11. ✅ replaceAll() – Replace all occurrences of one value with another


List<String> letters = new ArrayList<>([Link]("a", "b", "a", "c"));
[Link](letters, "a", "z");
[Link](letters); // Output: [z, b, z, c]

12. ✅ disjoint() – Check if two collections have no elements in common


List<Integer> a = [Link](1, 2, 3);
List<Integer> b = [Link](4, 5);
[Link]([Link](a, b)); // Output: true

✅ Summary Table
Method Use Case
sort() Sort list in ascending order
reverse() Reverse list
shuffle() Randomize order of list
swap(i, j) Swap two elements
max() / min() Get max/min values
frequency() Count occurrence of item
binarySearch() Search in sorted list
fill() Fill list with a single value
copy(dest, src) Copy one list into another
replaceAll() Replace all matching values
disjoint() Check if no common elements

You might also like