Java Collection Framework

How good are you in data structure?

If you have followed my data structure series then you must be good at data structure, but it's true that writing an optimized code to structure our data in a project to process them efficiently and faster is a great headache.

Now we have arrays where we can store bulk data but the array has limited features/functionality, I want my data as stack or queue or I don't know how much data will come(size is not predefined) from DB, maybe my data is coming in pairs. To get all these features we must write extra complicated Java code.

This can give you a headache.

Now what to do?

Don't worry, Java comes to the rescue with a Java Collection Framework which does almost all of your work related to storing data.

What is Java Collection Framework?

The Java Collection Framework is a fundamental part of Java programming, providing a comprehensive set of classes and interfaces to work with various types of data collections. Whether you're dealing with lists, sets, maps, or queues, the Collection Framework offers a wealth of functionalities to efficiently manage and manipulate data. This is a part of java.util package and there are two main interfaces java.util.Collection and java.util.Map. Below is the diagram showing all the classes and interfaces.

By seeing the above diagram you must have got some idea about Collection and Map interfaces and their implementing classes. If not, then don't worry I will explain each class in the next blog.

For now, let me explain about java.util.Collections class and before I start let me tell you it is different than java.util.Collection interface.

What is java.util.Collections class?

As we all know about java.util.Arrays class which helps to do some operations on arrays, similarly we have java.util.Collections to do some operations on collections.

Let's see some of the important operations that can be done by java.util.Collections class.

  1. Sorting Collections:

    • sort(List<T> list): Sorts the elements of the specified list in natural order.

    • sort(List<T> list, Comparator<? super T> c): Sorts the elements of the list using the specified comparator.

  2. Searching and Reversing:

    • binarySearch(List<? extends Comparable<? super T>> list, T key): Performs a binary search on the specified list for the given key.

    • reverse(List<?> list): Reverses the order of the elements in the specified list.

  3. Filling and Copying:

    • fill(List<? super T> list, T obj): Replaces all elements of the specified list with the specified object.

    • copy(List<? super T> dest, List<? extends T> src): Copies the elements from the source list to the destination list.

  4. Creating Immutable Collections:

    • unmodifiableCollection(Collection<? extends T> c): Returns an unmodifiable view of the specified collection.

    • unmodifiableList(List<? extends T> list): Returns an unmodifiable view of the specified list.

    • unmodifiableSet(Set<? extends T> s): Returns an unmodifiable view of the specified set.

  5. Synchronizing Collections:

    • synchronizedCollection(Collection<T> c): Returns a synchronized (thread-safe) view of the specified collection.

    • synchronizedList(List<T> list): Returns a synchronized view of the specified list.

    • synchronizedSet(Set<T> s): Returns a synchronized view of the specified set.

  6. Finding Minimum and Maximum:

    • min(Collection<? extends T> coll): Returns the minimum element in the given collection, according to the natural ordering.

    • max(Collection<? extends T> coll): Returns the maximum element in the given collection, according to the natural ordering.

  7. Frequency and Disjoint Checks:

    • frequency(Collection<?> c, Object o): Returns the number of occurrences of the specified element in the collection.

    • disjoint(Collection<?> c1, Collection<?> c2): Checks if the two collections have no elements in common.

  8. Creating Singleton Collections:

    • singleton(T o): Returns an immutable set containing only the specified object.

    • singletonList(T o): Returns an immutable list containing only the specified object.

  9. Miscellaneous:

    • emptySet(), emptyList(), emptyMap(): Returns empty immutable sets, lists, and maps.

    • checkedCollection(Collection<E> c, Class<E> type): Returns a dynamically type-checked collection.

Let me end this blog here, in the next blog we will explore each collection so stay tuned and stay connected.

Other blogs by me:

Learn Complete multithreading and thread pool in this series

Getting started with Multithreading in Java

A Comprehensive Guide to Thread Pools in Java

Learn data structure here

Did you find this article valuable?

Support Java Blogs By Hemant by becoming a sponsor. Any amount is appreciated!