[go: up one dir, main page]

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

Collections Class

The Collections class in Java, part of the java.util package, provides static utility methods for manipulating collections like List, Set, and Map. It includes common operations such as sorting, searching, reversing, and synchronization, along with methods for creating unmodifiable collections. Key points include that most methods modify the original collection and that many require mutable and non-null collections.

Uploaded by

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

Collections Class

The Collections class in Java, part of the java.util package, provides static utility methods for manipulating collections like List, Set, and Map. It includes common operations such as sorting, searching, reversing, and synchronization, along with methods for creating unmodifiable collections. Key points include that most methods modify the original collection and that many require mutable and non-null collections.

Uploaded by

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

The Collections class in Java is a part of the java.

util package and provides


static utility methods for operating on or returning collections such as List, Set,
and Map. This is different from the Collection interface, which is a root interface
in the Java Collection Framework.

✅ Overview of Collections Class:


Package: java.util

Type: Utility class (like Arrays class for arrays)

Contains: Static methods to operate on collections

Common Operations: Sorting, searching, reversing, shuffling, synchronization, etc.

🔧 Commonly Used Methods in Collections Class


1. Sorting
java
Copy
Edit
Collections.sort(list); // Sorts in natural order
Collections.sort(list, comparator); // Custom sorting using Comparator
2. Searching
java
Copy
Edit
Collections.binarySearch(list, key); // List must be sorted
3. Reversing
java
Copy
Edit
Collections.reverse(list);
4. Shuffling
java
Copy
Edit
Collections.shuffle(list); // Randomly permutes list elements
5. Swapping
java
Copy
Edit
Collections.swap(list, i, j); // Swap two elements
6. Filling
java
Copy
Edit
Collections.fill(list, value); // Replaces all elements with value
7. Copying
java
Copy
Edit
Collections.copy(destList, srcList); // Copies elements (dest must be >= src
size)
8. Finding Min/Max
java
Copy
Edit
Collections.min(list); // Based on natural order
Collections.max(list, comparator); // With Comparator
9. Replacing All Elements
java
Copy
Edit
Collections.replaceAll(list, oldVal, newVal);
10. Frequency Count
java
Copy
Edit
Collections.frequency(list, element); // Returns count of element in list
🔐 Synchronization Methods
These make collections thread-safe:

java
Copy
Edit
Collections.synchronizedList(list);
Collections.synchronizedSet(set);
Collections.synchronizedMap(map);
🪞 Unmodifiable Collections
Used to create read-only views:

java
Copy
Edit
Collections.unmodifiableList(list);
Collections.unmodifiableSet(set);
Collections.unmodifiableMap(map);
🧪 Example Program
java
Copy
Edit
import java.util.*;

public class CollectionsDemo {


public static void main(String[] args) {
List<String> fruits = new ArrayList<>(Arrays.asList("Banana", "Apple",
"Mango", "Orange"));

Collections.sort(fruits); // Sorting
System.out.println("Sorted: " + fruits);

Collections.reverse(fruits); // Reverse
System.out.println("Reversed: " + fruits);

Collections.shuffle(fruits); // Shuffle
System.out.println("Shuffled: " + fruits);

String maxFruit = Collections.max(fruits);


System.out.println("Max: " + maxFruit);

int freq = Collections.frequency(fruits, "Apple");


System.out.println("Frequency of Apple: " + freq);
}
}
📌 Key Points
Collections methods work mostly with List, Set, and Map.

Most methods modify the original collection.


Collections.sort() uses TimSort (dual-pivot quicksort).

Collections.unmodifiableXXX() is useful for security and encapsulation.

Many methods require collections to be mutable and non-null.

You might also like