From the course: Advanced Java Programming

The Collections framework in Java - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

The Collections framework in Java

- [Instructor] Collections are an essential part of the Java programming language. They let you group objects together, in a container, which you can iterate over, in Java. There are lots of different implementations of collections. Which type of collection you choose to use depends on your exact requirements. But, there are some key factors to bear in mind. Firstly, is the order important? Sometimes, it's fine to have all the entries in any random order. But, sometimes, it is important to be able to access them in a specific order. Secondly, are duplicates allowed? There are some situations where it is fine to have duplicate entries, and others, when every entry should be different. Another factor to consider is how fast it will be to perform operations. Different types of collections take different amounts of time to perform certain operations. Some are quick at retrieving entries. Some are quicker at adding and removing them. Finally, memory used is also a factor. Some types of collections take up more memory than others. A useful resource for choosing which collection to use is a flow chart available at this link. In Java, there is a set of interfaces that define different types of collection. At the very top of the hierarchy is Iterable. All types of collection implement Iterable, and it declares the forEach method. Then, there is the collection interface, which extends Iterable. This interface declares all of the methods that every collection must have. For example, add remove is empty, toArray, and so on. There are no classes that are a concrete implementation of collection directly. Before we get to concrete classes, there is another layer of interfaces. These include set, list, and queue. There are many more, but these are three of the most commonly used ones. These interfaces define a more specific set of behaviors about how the collection behaves. Sets are a type of collection that do not allow duplicate elements. They are also unordered. For example, if you are making a card game, and wanted to ensure that there are no duplicate cards in the game, you could store them in a set. Lists, on the other hand, allow duplicate entries. The order of elements in a list is also significant. When you iterate over a list, the items are not returned in a random order, as they would be with a set. Queues are a type of collection that lets you add elements to the heads of the collection. They typically use first-in, first-out operations. They are a bit like real life queues in a store, where the first person to arrive is the first person to be served. There is also another structure called maps. Maps do not actually extend the collection interface. This is because they contain key value pairs, which are not suited to being elements in a collection, however, they are still considered to be a part of the collections framework in Java. If you want to find out more about collections in general, a good place to start is Oracle's tutorials at these two links.

Contents