Introducing the collections framework

Imagine that you have to process a list of results submitted by registered users of your website for an opinion poll. You aren’t concerned about the order of receiving or processing these results, but you won’t accept multiple votes from the same user. Imagine that in another case, you’re creating a drawing application that includes an Undo button. You need to keep track of the order in which the drawing commands are selected, so you can undo the last command. Duplicate commands are allowed in a drawing application. Imagine yet another case, when you’re looking up a word in a dictionary. The words are ordered alphabetically, but duplicate words don’t exist in the dictionary.

These scenarios show examples of needing to store and retrieve collections of data in various manners. For one collection, you might need to retrieve data in the order in which it was generated. For another collection, you might not allow duplicate values but would prefer data to be sorted on a data item.

A collection is an object that can group other objects. Each object in a collection is referred to as an element. The Java collections framework is architecture for represent- ing and manipulating collections. This framework defines many interfaces to support the need for storing a collection of data in various formats. These collections might need to be ordered, to be sorted, to allow duplicate values, to be immutable, to be of fixed size, and more. The collections framework includes high-performance, high- quality implementations of useful data structures to store a collection of your objects. It includes the following:

  • Interfaces—Multiple interfaces like List, Set, Deque, and Map model the data structures used for storing, accessing, and manipulating a collection of data.

  • Implementations —Concrete classes like ArrayList, HashSet, and TreeMap imple-

    ment the interfaces.

  • Algorithms—Classes like Collections and Arrays contain utility methods like

    sort() and search() for sorting and searching List objects or arrays.

Don’t confuse the interface Collection with the class Collections. Collection is the base interface in the collections framework that is extended by most of the other interfaces. Class Collections defines utility methods to operate on or return collections.

But at the same time, using the collections framework can be overwhelming. The key to optimal use of the collections framework is to get the basics right. So, let’s start with the base interface in the Java collections framework: Collection.

Collections Framework :

Last updated