The java.util.Collections class consists of static methods that operate on or return collections. It provides a set of utility methods to perform operations like sorting, searching, reversing, and more on collections.
Key Points:
Utility Methods: Offers a broad array of methods for manipulating collections.
Algorithms: Provides sorting, searching, and shuffling methods, among others.
Immutability: Has methods to make collections unmodifiable.
Synchronization: Contains methods to return synchronized versions of collections.
Singletons: Can produce a collection with a single element.
Java.util.Collections Common Methods
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
// 1. Create a List of integers
List<Integer> numbers = new ArrayList<>();
numbers.add(3);
numbers.add(1);
numbers.add(4);
numbers.add(2);
System.out.println("1. Original List: " + numbers);
// 2. Sort the List
Collections.sort(numbers);
System.out.println("2. Sorted List: " + numbers);
// 3. Reverse the List
Collections.reverse(numbers);
System.out.println("3. Reversed List: " + numbers);
// 4. Find the maximum value in the List
int max = Collections.max(numbers);
System.out.println("4. Maximum Value: " + max);
// 5. Find the minimum value in the List
int min = Collections.min(numbers);
System.out.println("5. Minimum Value: " + min);
// 6. Shuffle the List
Collections.shuffle(numbers);
System.out.println("6. Shuffled List: " + numbers);
}
}
Output:
1. Original List: [3, 1, 4, 2] 2. Sorted List: [1, 2, 3, 4] 3. Reversed List: [4, 3, 2, 1] 4. Maximum Value: 4 5. Minimum Value: 1 6. Shuffled List: [Random Order, for example: [2, 1, 4, 3]]
Explanation:
1. Initialized an ArrayList of integers named numbers and populated it with some values.
2. Used the sort() method of Collections to order the list in ascending fashion.
3. Utilized the reverse() method of Collections to reverse the order of elements in the list.
4. Used the max() method to find the largest value in the list.
5. Applied the min() method to find the smallest value in the list.
6. Demonstrated the shuffle() method, which randomly rearranges the order of elements in the list.
java.util.Collections is a utility class that provides several methods to operate on collection objects. These methods help in manipulating and obtaining information from collection objects without the need for custom logic.
Java.util.Collections Searching Example
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SearchingDemo {
public static void main(String[] args) {
// 1. Create a list of integers
List<Integer> numbers = Arrays.asList(1, 3, 5, 7, 9);
System.out.println("1. List of numbers: " + numbers);
// 2. Search for a number in the list
int index = Collections.binarySearch(numbers, 5);
System.out.println("2. Index of number 5: " + index);
// 3. Search for a number not in the list
int absentIndex = Collections.binarySearch(numbers, 6);
System.out.println("3. Index when searching for absent number 6: " + absentIndex);
}
}
Output:
1. List of numbers: [1, 3, 5, 7, 9] 2. Index of number 5: 2 3. Index when searching for absent number 6: -4
Explanation:
1. Initialized a list of integers named numbers.
2. Made use of the Collections.binarySearch() method to search for the index of number 5 in the list. This method performs a binary search and requires the list to be sorted.
3. Tried searching for a number (6) not present in the list using Collections.binarySearch(). If the list doesn't contain the key, the method returns a negative value, which is -insertionPoint – 1. In this case, the insertion point is 3, so the returned value is -4.
It's essential to ensure the list is sorted before performing a binary search. Otherwise, the results are undefined. The Collections.sort() method can be employed to sort the list if it's not already sorted.
Using Java.util.Collections to Ensure Thread Safety
The synchronizedList() method of java.util.Collections provides a simple way to make a list thread-safe without having to manage explicit locks or synchronization blocks. It's especially useful in scenarios where there's a need to share a list across multiple threads and ensure data consistency.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ThreadSafeListDemo {
public static void main(String[] args) {
// 1. Create a List of integers
List<Integer> numbers = new ArrayList<>();
numbers.add(3);
numbers.add(1);
numbers.add(4);
System.out.println("1. Original List: " + numbers);
// 2. Convert the List to a thread-safe version
List<Integer> synchronizedNumbers = Collections.synchronizedList(numbers);
System.out.println("2. Created a thread-safe version of the list.");
// 3. Use the synchronized list in multiple threads
Thread thread1 = new Thread(() -> {
synchronizedNumbers.add(2);
});
Thread thread2 = new Thread(() -> {
synchronizedNumbers.add(5);
});
thread1.start();
thread2.start();
// Wait for threads to complete
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("3. Updated List after multi-threading: " + synchronizedNumbers);
}
}
Output:
1. Original List: [3, 1, 4] 2. Created a thread-safe version of the list. 3. Updated List after multi-threading: [3, 1, 4, 2, 5] (order might vary)
Explanation:
1. Initialized an ArrayList of integers named numbers and populated it with some values.
2. Used the synchronizedList() method of Collections to create a thread-safe version of the list. This wrapped list can safely be accessed by multiple threads concurrently.
3. Demonstrated how to use the thread-safe list by starting two threads that both add values to the synchronizedNumbers list. Since the list is synchronized, it prevents concurrent modification exceptions and other synchronization-related issues.
Java.util.Collections Singletons Example
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SingletonDemo {
public static void main(String[] args) {
// 1. Create a singleton list
List<String> singleItemList = Collections.singletonList("Item1");
System.out.println("1. Singleton List: " + singleItemList);
// 2. Create a singleton set
Set<String> singleItemSet = Collections.singleton("Item2");
System.out.println("2. Singleton Set: " + singleItemSet);
// 3. Create a singleton map
Map<String, String> singleItemMap = Collections.singletonMap("Key", "Value");
System.out.println("3. Singleton Map: " + singleItemMap);
// Uncommenting the next line will throw an UnsupportedOperationException
// singleItemList.add("Another Item");
}
}
Output:
1. Singleton List: [Item1] 2. Singleton Set: [Item2] 3. Singleton Map: {Key=Value}
Explanation:
1. Utilized the Collections.singletonList() method to create an immutable list containing a single item.
2. Employed the Collections.singleton() method to create an immutable set with a single item.
3. Made use of the Collections.singletonMap() to generate an immutable map with a single key-value pair.
It's worth noting that the singleton collections returned by these methods are immutable, meaning you can't add or remove elements from them. Attempting to do so, as illustrated in the commented code, will result in an UnsupportedOperationException.