📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The Java Collections Class is a utility class in java.util package that consists of several static methods that operate on or return collections. It is a member of the Java Collections Framework.
Important Key Points About Collections Class
Utilities:
Algorithms:
Synchronization Wrappers:
Unmodifiable Wrappers:
Singletons:
Checked Interfaces:
Finding Extreme Values:
Frequency and Disjoint:
Empty and Singleton Collections:
Search Operations:
Important java.util.Collections methods
- sort(List list)
- sort(List list, Comparator<? super Project> c)
- shuffle(List<?> list)
- reverse(List<?> list)
- rotate(List<?> list, int distance)
- swap(List<?> list, int i, int j)
- replaceAll(List list, String oldVal, String newVal)
- copy(List<? super String> dest, List<? extends String> src)
- Collections.binarySearch(list, "element 4")
- frequency(Collection<?> c, Object o)
- disjoint(Collection> c1, Collection> c2)
- min(Collection extends ?> coll)
- max(Collection extends ?> coll)
- Sorting
- Shuffling
- Routine Data Manipulation
- Searching
- Composition
- Finding Extreme Values
Sorting using Collections Utility Class
sort(List list)
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 4");
list.add("element 3");
// Sorts the specified list into ascending order, according to
// the natural ordering of its elements.
Collections.sort(list);
for (String str : list) {
System.out.println("sort elements in ascending order --" + str);
}
}
}
sort elements in ascending order --element 1
sort elements in ascending order --element 2
sort elements in ascending order --element 3
sort elements in ascending order --element 4
sort(List list, Comparator<? super Project> c)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CollectionsClassExamples {
public static void main(String[] args) {
sortingCustomObjectsByComparator();
}
private static void sortingCustomObjectsByComparator() {
// Sort Projects by project id in ascending order.
List<Project> projects = new ArrayList<>();
Project project = new Project();
project.setProjectId(100);
project.setProjectName("TMS");
projects.add(project);
Project project2 = new Project();
project2.setProjectId(200);
project2.setProjectName("APEX");
projects.add(project2);
Project project3 = new Project();
project3.setProjectId(50);
project3.setProjectName("CMS");
projects.add(project3);
// Sorting project by project name in ascending order in Java
Collections.sort(projects, Comparator.comparing(Project::getProjectName));
printList(projects);
}
private static void printList(List<Project> projects) {
for (Project project : projects) {
System.out.println(project.getProjectId());
System.out.println(project.getProjectName());
}
}
}
class Project implements Comparable<Project> {
private int projectId;
private String projectName;
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
@Override
public int compareTo(Project project) {
return this.getProjectId() - project.getProjectId();
}
}
200
APEX
50
CMS
100
TMS
Shuffling using Collections Utility Class
shuffle(List<?> list)
private static void shuffleAlgorithmsDemo() {
List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 4");
list.add("element 3");
Collections.sort(list);
for (String str : list) {
System.out.println(" sort elements in ascending order --" + str);
}
// randomly permutes the elements in a List.
Collections.shuffle(list);
for (String str : list) {
System.out.println(" sort elements in ascending order --" + str);
}
}
sort elements in ascending order --element 1
sort elements in ascending order --element 2
sort elements in ascending order --element 3
sort elements in ascending order --element 4
shuffle elements --element 1
shuffle elements --element 3
shuffle elements --element 4
shuffle elements --element 2
Routine Data Manipulation
- reverse - reverses the order of the elements in a List.
- fill - overwrites every element in a list with the specified value. This operation is useful for reinitializing a List.
- copy - takes two arguments, a destination List, and a source list, and copies the elements of the source into the destination, overwriting its contents. The destination List must be at least as long as the source. If it is longer, the remaining elements in the destination List are unaffected.
- swap - swaps the elements at the specified positions in a List.
- addAll - Adds all the specified elements to a Collection. The elements to be added may be specified individually or as an array.
reverse() Method
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 4");
list.add("element 3");
Collections.sort(list);
for(String str : list){
System.out.println("sort elements in ascending order --" + str);
}
//reverses the order of the elements in a List.
Collections.reverse(list);
for(String str : list){
System.out.println(" reverse sorted elements --" + str);
}
}
}
Output:
sort elements in ascending order --element 1
sort elements in ascending order --element 2
sort elements in ascending order --element 3
sort elements in ascending order --element 4
reverse sorted elements --element 4
reverse sorted elements --element 3
reverse sorted elements --element 2
reverse sorted elements --element 1
rotate() Method
This method is a built-in method in Java's Collections class that rotates the elements in a specified list by a specified distance. If the distance is positive, the elements are shifted to the right; if the distance is negative, the elements are shifted to the left.Here is an example usage of the rotate method:
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
// Creating an ArrayList
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
// Displaying elements of original list
System.out.println("List before rotation: " + list);
// Using Collections.rotate() to rotate the list by 2 positions to the right
Collections.rotate(list, 2);
// Displaying elements of list after rotation
System.out.println("List after rotation: " + list);
}
}
Output:
List before rotation: [1, 2, 3, 4, 5]
List after rotation: [4, 5, 1, 2, 3]
swap() Method
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
// Displaying elements of original ArrayList
System.out.println("ArrayList before the Swap:");
for(String str: list){
System.out.println(str);
}
// Using swap() method to swap elements of ArrayList
Collections.swap(list, 1, 4);
System.out.println("\nArrayList after the Swap:");
for(String str: list){
System.out.println(str);
}
}
}
Output:
ArrayList before the Swap:
A
B
C
D
E
ArrayList after the Swap:
A
E
C
D
B
replaceAll() Method
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 4");
list.add("element 3");
//replaces all occurrences of one specified value with another.
Collections.replaceAll(list, "element 3", "element 6");
// Displaying elements of list after replace
System.out.println("List after replace element 6: " + list);
}
}
Output:
List after replace element 6: [element 2, element 1, element 4, element 6]
copy()
This method is a built-in method in Java's Collections class that copies all elements from one list into another. The destination list needs to be large enough to accommodate all elements from the source list, otherwise an IndexOutOfBoundsException will be thrown.import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
// Create a source list
ArrayList<String> src = new ArrayList<String>();
src.add("A");
src.add("B");
src.add("C");
// Create a destination list with enough capacity
ArrayList<String> dest = new ArrayList<String>();
dest.add("");
dest.add("");
dest.add("");
// Displaying elements of original lists
System.out.println("Source List: " + src);
System.out.println("Destination List (Before Copy): " + dest);
// Use Collections.copy() method to copy elements
Collections.copy(dest, src);
System.out.println("Destination List (After Copy): " + dest);
}
}
Output:
Source List: [A, B, C]
Destination List (Before Copy): [, , ]
Destination List (After Copy): [A, B, C]
Searching using Collections Utility Class
The binary search algorithm searches for a specified element in a sorted list. This algorithm has two forms:- The first takes a List and an element to search for (the "search key"). This form assumes that the list is sorted in ascending order according to the natural ordering of its elements.
- The second form takes a Comparator in addition to the List and the search key and assumes that the list is sorted into ascending order according to the specified Comparator.
private static void searchingAlgorithmsDemo() {
List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 4");
list.add("element 3");
Collections.sort(list);
for (String str : list) {
System.out.println(" sort elements in ascending order --" + str);
}
int index = Collections.binarySearch(list, "element 4");
System.out.println("Element found at ::" + index);
}
sort elements in ascending order --element 1
sort elements in ascending order --element 2
sort elements in ascending order --element 3
sort elements in ascending order --element 4
Element found at ::3
Composition
- frequency - counts the number of times the specified element occurs in the specified collection.
- disjoint - determines whether two Collections are disjoint; that is, whether they contain no elements in common.
frequency() Method
List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 1");
list.add("element 3");
//Returns the number of elements in the specified collection
//equal to the specified object.
System.out.println(Collections.frequency(list, "element 1"));
disjoint() Method
List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 1");
list.add("element 3");
//Returns the number of elements in the specified collection
//equal to the specified object.
System.out.println(Collections.frequency(list, "element 1"));
List<String> list2 = new LinkedList<>();
list2.add("element 2");
list2.add("element 1");
list2.add("element 1");
list2.add("element 3");
//Returns true if the two specified collections have no elements in common.
System.out.println(Collections.disjoint(list, list2));
Finding Extreme Values(min and max methods)
min() Method
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(100);
list.add(300);
list.add(200);
list.add(500);
System.out.println("Minimun element in the list: " + Collections.min(list));
}
}
Output:
Minimun element in the list: 100
max() Method
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> list = new LinkedList<>();
list.add(100);
list.add(300);
list.add(200);
list.add(500);
System.out.println("Maximum element in the list: " + Collections.max(list));
}
}
Output:
Maximum element in the list: 500
Comments
Post a Comment
Leave Comment