Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
The Collections
class in Java is a utility class that provides several static methods for working with collections. These methods help in performing various operations like sorting, searching, reversing, and synchronizing collections.
In this tutorial, we will explore some of the most commonly used methods in the Collections
class.
The Collections.sort()
method is used to sort a list in ascending order.
Example:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(5); numbers.add(3); numbers.add(1); Collections.sort(numbers); System.out.println("Sorted List: " + numbers); // Output: Sorted List: [1, 3, 5] } }
The Collections.binarySearch()
method is used to search for an element in a sorted list using the binary search algorithm. The list must be sorted in ascending order before using this method.
Example:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(3); numbers.add(5); int index = Collections.binarySearch(numbers, 3); System.out.println("Index of 3: " + index); // Output: Index of 3: 1 } }
The Collections.reverse()
method is used to reverse the order of elements in a list.
Example:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(3); numbers.add(5); Collections.reverse(numbers); System.out.println("Reversed List: " + numbers); // Output: Reversed List: [5, 3, 1] } }
The Collections.synchronized*()
methods are used to create synchronized (thread-safe) versions of various collection objects, such as lists, sets, and maps.
Example:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(3); numbers.add(5); List<Integer> synchronizedList = Collections.synchronizedList(numbers); } }
The Collections.min()
and Collections.max()
methods are used to find the minimum and maximum elements in a collection, respectively.
Example:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(5); numbers.add(3); numbers.add(1); int min = Collections.min(numbers); int max = Collections.max(numbers); System.out.println("Minimum: " + min); // Output: Minimum: 1 System.out.println("Maximum: " + max); // Output: Maximum: 5 } }
In summary, the Collections
class in Java provides various utility methods for working with collections. These methods help in performing common operations like sorting, searching, reversing, synchronizing collections, and finding minimum and maximum elements.
Manipulating Collections with Java Collections Class:
The Collections
class provides utility methods for common operations on collections, such as sorting, shuffling, and searching.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class CollectionsExample { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(5); numbers.add(2); numbers.add(8); // Manipulating collections Collections.sort(numbers); Collections.reverse(numbers); Collections.shuffle(numbers); System.out.println(numbers); // Shuffled list } }
addAll Method in Java Collections Class:
The addAll
method is used to add all elements from one collection to another.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class AddAllExample { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); Collections.addAll(fruits, "Apple", "Banana", "Orange"); System.out.println(fruits); // [Apple, Banana, Orange] } }
Sorting Collections Using Collections.sort in Java:
The Collections.sort
method is used to sort a collection in ascending order.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class SortExample { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(5); numbers.add(2); numbers.add(8); Collections.sort(numbers); System.out.println(numbers); // [2, 5, 8] } }
Removing Elements from Collections with Collections.remove:
The Collections.remove
method is used to remove all occurrences of a specified element from a collection.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class RemoveExample { public static void main(String[] args) { List<String> colors = new ArrayList<>(); colors.add("Red"); colors.add("Blue"); colors.add("Red"); Collections.remove(colors, "Red"); System.out.println(colors); // [Blue] } }
Java Collections Reverse Order with Collections.reverse:
The Collections.reverse
method is used to reverse the order of elements in a collection.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class ReverseExample { public static void main(String[] args) { List<String> fruits = new ArrayList<>(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); Collections.reverse(fruits); System.out.println(fruits); // [Orange, Banana, Apple] } }
Searching Elements in Collections Using Collections.binarySearch:
The Collections.binarySearch
method is used to perform a binary search on a sorted list.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class BinarySearchExample { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(2); numbers.add(5); numbers.add(8); int index = Collections.binarySearch(numbers, 5); System.out.println("Index of 5: " + index); // 1 } }
Shuffling Elements in Java Collections with Collections.shuffle:
The Collections.shuffle
method is used to shuffle the elements of a collection.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class ShuffleExample { public static void main(String[] args) { List<String> cards = new ArrayList<>(); cards.add("Heart"); cards.add("Diamond"); cards.add("Spade"); cards.add("Club"); Collections.shuffle(cards); System.out.println(cards); // Shuffled cards } }
Finding Minimum and Maximum in Collections Using Collections.min and Collections.max:
The Collections.min
and Collections.max
methods are used to find the minimum and maximum elements in a collection.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class MinMaxExample { public static void main(String[] args) { List<Integer> numbers = new ArrayList<>(); numbers.add(2); numbers.add(5); numbers.add(8); int min = Collections.min(numbers); int max = Collections.max(numbers); System.out.println("Min: " + min); // 2 System.out.println("Max: " + max); // 8 } }
Java Collections Frequency and Occurrences with Collections.frequency:
The Collections.frequency
method is used to find the frequency (occurrences) of an element in a collection.
import java.util.Collections; import java.util.List; import java.util.ArrayList; public class FrequencyExample { public static void main(String[] args) { List<String> colors = new ArrayList<>(); colors.add("Red"); colors.add("Blue"); colors.add("Red"); int frequency = Collections.frequency(colors, "Red"); System.out.println("Frequency of Red: " + frequency); // 2 } }