English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn the different algorithms provided by the Java Collections Framework through examples.
The Java Collections Framework provides various algorithms that can be used to process elements stored in data structures.
Static methods in Java are used to perform various operations on collections.
Since the algorithm can be used for various collections, it is also calledGeneral algorithm.
Let's see the implementation of different methods available in the collection framework.
The sort() method provided by the collection framework is used to sort elements. For example,
import java.util.ArrayList; import java.util.Collections; class Main { public static void main(String[] args) { //Create an array list ArrayList<Integer> numbers = new ArrayList<>(); //Add elements numbers.add(4); numbers.add(2); numbers.add(3); System.out.println("Unsorted ArrayList: ", + numbers); // Use sort() method Collections.sort(numbers); System.out.println("Sorted ArrayList: ", + numbers); } }
Output Result
Unsorted ArrayList: [4, 2, 3] Sorted ArrayList: [2, 3, 4]
Here, sorting is done in natural order (ascending). However, we can use the Comparator interface to customize the sorting order of the sort() method.
For more information, please visitJava Sorting.
The shuffle() method of the Java Collections framework is used to randomize the order of elements in any data structure. Its function is exactly opposite to sorting. For example
import java.util.ArrayList; import java.util.Collections; class Main { public static void main(String[] args) { //Create an array list ArrayList<Integer> numbers = new ArrayList<>(); //Add elements numbers.add(1); numbers.add(2); numbers.add(3); System.out.println("Sorted ArrayList: ", + numbers); //Use shuffle() method Collections.shuffle(numbers); System.out.println("Use shuffle ArrayList: ", + numbers); } }
Output Result
Sorted ArrayList: [1, 2, 3] Use shuffle ArrayList: [2, 1, 3]
When we run the program, the shuffle() method will return random output.
Shuffle algorithm is mainly used in games that require random output.
In Java, the collection framework provides various methods for handling data.
reverse() - Reverse the order of elements
fill() - Replace each element in the collection with a specified value
copy() - Create a copy of elements from a specified source to a target
swap() - Swap the positions of two elements in the collection
addAll() - Add all elements of the collection to another collection
For example,
import java.util.Collections; import java.util.ArrayList; class Main { public static void main(String[] args) { //Create an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); System.out.println("ArrayList1: \ + numbers); // Use reverse() Collections.reverse(numbers); System.out.println("Reverse ArrayList1: \ + numbers); // Use swap() Collections.swap(numbers, 0, 1); System.out.println("ArrayList1 Use swap(): " + numbers); ArrayList<Integer> newNumbers = new ArrayList<>(); // Use addAll newNumbers.addAll(numbers); System.out.println("ArrayList2 using addAll(): \ + newNumbers); // using fill() Collections.fill(numbers, 0); System.out.println("ArrayList1 using fill(): " + numbers); // using copy() Collections.copy(newNumbers, numbers); System.out.println("ArrayList2 using copy(): " + newNumbers); } }
Output Result
ArrayList1: [1, 2] reverse ArrayList1: [2, 1] ArrayList1 using swap(): [1, 2] ArrayList2 using addAll(): [1, 2] ArrayList1 using fill(): [0, 0] ArrayList2 using copy(): [0, 0]
Note:The sizes of the two lists should be the same when executing the copy() method.
The binarySearch() method of the Java Collections Framework searches for the specified element. It returns the position of the element in the specified collection. For example,
import java.util.Collections; import java.util.ArrayList; class Main { public static void main(String[] args) { //Create an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); // using binarySearch() int pos = Collections.binarySearch(numbers, 3); System.out.println("3the position is \ + pos); } }
Output Result
3the position is 2
Note:Before executing the binarySearch() method, the collection should be sorted.
For more information, please visitJava Binary Search.
frequency() - returns the count of the number of times an element exists in the set
disjoint() - to check if two sets contain some common elements
For example,
import java.util.Collections; import java.util.ArrayList; class Main { public static void main(String[] args) { //Create an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); numbers.add(2); System.out.println("ArrayList1: \ + numbers); int count = Collections.frequency(numbers, 2); System.out.println("Count of 2: \ + count); ArrayList<Integer> newNumbers = new ArrayList<>(); newNumbers.add(5); newNumbers.add(6); System.out.println("ArrayList2: \ + newNumbers); boolean value = Collections.disjoint(numbers, newNumbers); System.out.println("Are the two lists disjoint? ", + value); } }
Output Result
ArrayList1: [1, 2, 3, 2] Count of 2: 2 ArrayList2: [5, 6] two lists are disjoint? true
The min() and max() methods of the Java Collections Framework are used to find the minimum and maximum elements, respectively. For example,
import java.util.Collections; import java.util.ArrayList; class Main { public static void main(String[] args) { //Create an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.add(3); // Using min() int min = Collections.min(numbers); System.out.println("Minimum Element: "); + min); // Using max() int max = Collections.max(numbers); System.out.println("Maximum Element: "); + max); } }
Output Result
Minimum Element: 1 Maximum Element: 3