Do you know the famous formula in programming said by Niklaus Wirth? Here is it:

Data Structures + Algorithms = Programs

That means a computer program is a combination of data structures that hold information in an organized manner, along with algorithms which process the information to produce desired results or outputs.

In any software programs, data structures and algorithms are the fundamental and primary elements that make them functioning.

If you studied computer engineering in college, you are probably familiar with the concepts of data structures like linked list, queue, stack, tree, etc. And remember you were given exercises to implement these data structures using C/C++ programming languages?

In the Java programming language, exactly in the JDK, you don’t have to re-invent the wheel, i.e. implementing data structures and algorithms from scratch, as the JDK provides a powerful, high quality and high performance reusable data structures and algorithms in a so-called Java Collections Framework.

 

1. What is a Collection?

In terms of programming, a collection is a data structure that holds a set of objects in a specific manner. It looks like arrays but collections are more advanced and more flexible. An array simply stores a fixed number of objects, whereas a collection stores objects dynamically, i.e. you can add or remove objects as you wish.

A collection also provides useful operations such as adding, removing, retrieving objects.

 

2. What is Java Collections Framework?



In short, Java Collections Framework is a set of reusable data structures and algorithms which are designed to free programmers from implementing data structures themselves so that they can focus on business logics.

The Java Collections Framework provides common data structures implementations which are enough for general-purpose such as list, set, map, queue, tree, etc. These collections are high-performance, high-quality, and easy to use with very good documentation.

In addition, the Java Collections Framework provides useful and robust algorithms such as searching and sorting on collections, and the interoperability between collections and arrays.

 

3. Why Use Java Collections Framework?

The Java Collections Framework is a standard API which is used extensively in Java programming. It’s the standard and perfect API for manipulating collections because of the following primary benefits:

 

4. How to Use Java Collections Framework?

The Java Collections Framework is easy to learn and use. Here’s an example of using a List collection:

List numbers = new ArrayList();
numbers.add(new Integer(1));
numbers.add(new Integer(5));
This code adds two integer numbers into a list collection. However, that’s the old fashion code which is before Java 1.5. Now everyone is using collections with generics like this:

List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(5);
 

For your convenience, here are some official and useful resources that help you study the Java Collections Framework:

 

If you prefer reading books, I recommend the following book written by Naftalin & Philip Wadler: Java Generics & Collections - a comprehensive guide about generics and collections in Java.

 

Complete Collections Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.