Introduction
Java Collections framework provides a comprehensive set of interfaces and classes to handle collection efficiently. Some lesser-known features are provided by Collections utils.
In this article, we will explore these lesser-known features with simple examples.
🚀 Unlock Your Path to Success with the Ultimate Everything Bundle! 🚀
Introducing the Everything Bundle — your one-stop solution to mastering Java, Spring Boot, SQL, acing interviews, and securing certifications!
1. Collections.nCopies()
This method in Java returns an immutable list containing ncopies of specified objects.
Internal code for ncopies() which is part of Collections utils.
public static <T> List<T> nCopies(int n, T o) {
if (n < 0)
throw new IllegalArgumentException("List length = " + n);
return new CopiesList<>(n, o);
}
Example:
public static void nCopies(){
List<String> tests = Collections.nCopies(10, "test");
System.out.println(tests);
}
[test, test, test, test, test, test, test, test, test, test]
2. Collections.frequency()
This method in Java is used to find the frequency of a specified element in the given collection.
Internal code:
public static int frequency(Collection<?> c, Object o) {
int result = 0;
if (o == null) {
for (Object e : c)
if (e == null)
result++;
} else {
for (Object e : c)
if (o.equals(e))
result++;
}
return result;
}
Example:
public static void frequency(){
List<Integer> integers = List.of(1, 2, 3, 4, 5, 1, 2, 3, 2, 3, 4);
int frequency = Collections.frequency(integers, 3);
System.out.println(frequency);
}
3
3. Collections.disjoint()
This method in Java provides a way to check if two collections have any common element, if yes then it returns true, otherwise false. With this feature, developers can quickly find if there are common elements present in the collection without iterating over them.
public static boolean disjoint(Collection<?> c1, Collection<?> c2) { }
Example:
public static void disjoint(){
List<Integer> integers = List.of(1, 2, 3, 4);
List<Integer> integers1 = List.of(5, 6);
boolean disjoint = Collections.disjoint(integers1, integers);
System.out.println(disjoint);
List<Integer> integers2 = List.of(1, 2, 3, 4);
boolean disjoint1 = Collections.disjoint(integers2, integers);
System.out.println(disjoint1);
}
true
false
4. Collections.singleton()
This method in Java is used to create an immutable set that contains only a single element. This method returns an immutable Set containing only a single element. If we try to add or remove an element we will get an exception.
Internal code:
public static <T> Set<T> singleton(T o) {
return new SingletonSet<>(o);
}
Example:
public static void singleton(){
Set<String> singleElement = Collections.singleton("Hello world");
System.out.println(singleElement);
singleElement.add("test");
}
[Hello world]
Exception in thread "main" java.lang.UnsupportedOperationException
at java.base/java.util.AbstractCollection.add(AbstractCollection.java:251)
at misc.JavaCollectionFeatures.singleton(JavaCollectionFeatures.java:41)
at misc.JavaCollectionFeatures.main(JavaCollectionFeatures.java:13)
5. Collections.rotate()
This method in Java rotates the elements of the specified list by a specified distance.
The method performs a “circular rotation” of the elements in the list, effectively shifting them either to the left or right based on a specified distance.
Internal code:
public static void rotate(List<?> list, int distance) {
if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
rotate1(list, distance);
else
rotate2(list, distance);
}
Example:
public static void rotate(){
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 3, 5, 5, 6);
Collections.rotate(integers, 6);
System.out.println(integers);
List<Integer> integers1 = Arrays.asList(1, 2, 3, 4, 5, 3, 5, 5, 6);
Collections.rotate(integers1, 10);
System.out.println(integers1);
List<Integer> integers2 = Arrays.asList(1, 2, 3, 4, 5, 3, 5, 5, 6);
Collections.rotate(integers2, -3);
System.out.println(integers2);
}
[4, 5, 3, 5, 5, 6, 1, 2, 3]
[6, 1, 2, 3, 4, 5, 3, 5, 5]
[4, 5, 3, 5, 5, 6, 1, 2, 3]
Conclusion
In this article, we explored lesser-known features of Collections utility. While the Java Collections utility provides a rich set of features, there are some lesser-known features that developers might find handy in some use cases.
Become Better Dev:
To upgrade your developer skills check below resources:
Get Ready For Spring Boot Interview. | Java Interview.
Upskill SQL by Practicing it ( 20 % off with code: ABNEW20OFF )