Collections in Java

Basics of collections...

When we think about a collection, it is nothing more than a container. In java programming we can describe a collection as an object that holds multiple elements into a single unit. Collections are used to store data, retrieve, manipulate, and communicate aggregate data with certain methods provided.

Standard definition of Collection framework “A collections framework is a unified architecture for representing and manipulating collections”.

 

Advantages of Java Collection-

Some of its benefits are:

Reduced programming effort: Collection framework provides a lot of data structures and easy implemented functions that certainly reduce the programming effort and give opportunity to programmer to concentrate on the important parts of program rather than on the low-level details required to make it work.

Increase quality and speed: This Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms. Collections methods ensure the test operability and adopt certain algorithms that definitely speed up the execution of the program so that we have more time to devote to improving programs' quality and performance.

Consistency: Many APIs naturally take collections on input and furnish them as output. In the past, each such API had a different implementation structure.

As I state in my video about the Java collections framework, the word "collection" unfortunately has several meanings in Java:

  1. a compilation or group of things
  2. Java Collections Framework
  3. a data structure
  4. java.util.Collection interface
  5. java.util.Collections

I assume you are referring either to the data structure or the collection interface.

The collection data structure allows to store a number of objects without any guaranteed order. You can add and remove elements elements from a collection, and you can iterate over a collection.

java.util.Collection is an interface that defines a contract for a collection data-structure in Java. All classes that implement a collection data-structure should implement this interface.

Classes of the JDK that implement this interface are for example: HashSet, LinkedHashSet, TreeSet, ArrayList, LinkedList and PriorityQueue.

This is one of the most confusing java interview question asked many a times to java freshers. Most of time, this question has been asked to java freshers to check their basic knowledge about the Java Collection Framework. This question seems confusing because both “Collection” and “Collections” look similar. Both are part of java collection framework, but both serve different purpose. Collection is a top level interface of java collection framework where as Collections is an utility class. In this article, we will discuss the differences between Collection and Collections in java.

Collection is a root level interface of the Java Collection Framework. Most of the classes in Java Collection Framework inherit from this interface. List, Set and Queue are main sub interfaces of this interface. JDK doesn’t provide any direct implementations of this interface. But, JDK provides direct implementations of it’s sub interfaces. ArrayList, Vector, HashSet, LinkedHashSet, PriorityQueue are some indirect implementations of Collection interface. Map interface, which is also a part of java collection framework, doesn’t inherit from Collection interface. Collection interface is a member of java.util package.

Collections is an utility class in java.util package. It consists of only static methods which are used to operate on objects of type Collection. For example, it has the method to find the maximum element in a collection, it has the method to sort the collection, it has the method to search for a particular element in a collection. Below is the list of some important methods of Collections class.

There are lot of scenarios where Collections are helpful.

The Java collections are nothing but a set of classes and interfaces that implement commonly reusable collection data structures like Arrays, lists, sets, queues, stacks and dictionaries. And there is one more type that is map which is not implements Collection interface but come under Collections framework.

Along with the collections classes there are several other classes and interfaces which are part of Collection Framework used to manipulated the collection data.

Below I’m listing the scenarios where we need to use collection classes.

  1. Fetch data from database and store each record into suitable collection class.
  2. Hold the data and transfer the data from UI to database and vice versa.
  3. Sorting the entity objects for example sorting of Employee object based on the Emp_Id or Emp_Name etc. (Assume Employee class have properties Emp_Id and Emp_Name.)
  4. Removing of the duplicate data (String or Objects) by using sets.
  5. Search the data in the collections class
  6. Storing the objects in key value pair.

There are so many scenarios where we can use collections. The listed are primary one.

I think this helps you to clear idea about collections...