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:

  • Reduce programming effort: with the reusable and useful data structures and algorithms, the programmers do not have to re-invent the wheel, thus they can devote their time on developing application’s business.

     

  • Increase program speed and quality: the concrete collections implemented by Java Collections Framework are built for high performance and high quality, thus programmers can take this advantage into their programs.

     

  • Foster software reuse: due to the Java Collections Framework is built into the JDK, code written using collections framework can be re-used every where among applications, libraries and APIs. That cuts development cost and increases interoperability among Java programs.

 

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:

  • Trail: Collections in the Java Tutorials: this is the most comprehensive and details guide about Java collections framework which is published by Oracle - the steward of Java technology. You should read this tutorial at least one time in your life, as I highly recommend.

     

  • Java Collections API Reference: this is the API documentation that allows you to quickly consult any interfaces, classes and methods of the Collections Framework. And you will have to use this resource in your daily coding.

     

  • And here are my tutorials on CodeJava.net for Java Collections framework: this gives you practical examples and useful tips for using collections and generics.

 

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 began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#1Girma2021-10-09 15:16
The way you are teaching is excellent.Can you teach us online for a group?
Quote