Discover Hidden Gems in Java Collections: 5 Features Unveiled
Written on
Chapter 1: Introduction to Java Collections
The Java Collections framework offers a robust array of interfaces and classes for efficient collection management. Within this framework, there are several lesser-known features that can significantly benefit developers, which we will examine through straightforward examples.
Section 1.1: Collections.nCopies()
The Collections.nCopies(int n, T o) method generates an immutable list comprising n copies of a specified object.
Here’s how it works internally:
public static List nCopies(int n, T o) {
if (n < 0)
throw new IllegalArgumentException("List length = " + n);return new CopiesList<>(n, o);
}
Example usage:
public static void nCopiesExample() {
List<String> tests = Collections.nCopies(10, "test");
System.out.println(tests);
}
Output:
[test, test, test, test, test, test, test, test, test, test]
Section 1.2: Collections.frequency()
This method, Collections.frequency(Collection c, Object o), is useful for determining how often a specific element appears within a given collection.
The internal logic looks like this:
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;
}
To see it in action:
public static void frequencyExample() {
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);
}
Output:
3
Section 1.3: Collections.disjoint()
The Collections.disjoint(Collection c1, Collection c2) method checks whether two collections share any elements. It returns true if they do not, and false otherwise.
Example code:
public static void disjointExample() {
List<Integer> integers = List.of(1, 2, 3, 4);
List<Integer> integers1 = List.of(5, 6);
boolean areDisjoint = Collections.disjoint(integers1, integers);
System.out.println(areDisjoint);
List<Integer> integers2 = List.of(1, 2, 3, 4);
boolean areNotDisjoint = Collections.disjoint(integers2, integers);
System.out.println(areNotDisjoint);
}
Output:
true
false
Section 1.4: Collections.singleton()
The Collections.singleton(T o) method creates an immutable set that contains just one element. It throws an exception if any attempt is made to add or remove elements.
Here’s the internal implementation:
public static Set singleton(T o) {
return new SingletonSet<>(o);
}
Example demonstration:
public static void singletonExample() {
Set<String> singleElement = Collections.singleton("Hello world");
System.out.println(singleElement);
singleElement.add("test"); // This line will throw an exception
}
Output:
[Hello world]
Exception in thread "main" java.lang.UnsupportedOperationException
Section 1.5: Collections.rotate()
The Collections.rotate(List list, int distance) method shifts the elements of a specified list by a defined distance, effectively enabling a circular rotation.
Here’s how it works:
public static void rotate(List list, int distance) {
if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
rotate1(list, distance);else
rotate2(list, distance);
}
Example usage:
public static void rotateExample() {
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);
}
Output:
[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]
Chapter 2: Conclusion
In this article, we have delved into several underappreciated features of the Collections utility in Java. While the Java Collections framework is rich in functionality, these lesser-known features can prove invaluable in specific scenarios.
Become a Better Developer:
To enhance your programming skills, explore the resources below:
- Prepare for Spring Boot Interviews.
- Master Spring Boot 3 & Spring Framework 6 with Java.
- Level up your SQL skills by practicing with a special offer (20% off with code: ABNEW20OFF).
Don't forget to subscribe to our Java newsletter for ongoing updates related to Java and Spring Boot!
This video titled "Java Collections Features | Exploring Hidden methods for Developers | @Javatechie" uncovers various useful yet lesser-known features of the Java Collections framework that can elevate your coding practices.
The second video, "Master Java Collections Framework in 3 Hours | Full Course in Depth," offers a comprehensive course that dives deep into the Java Collections framework, perfect for developers looking to master this crucial aspect of Java.