In this chapter you will learn:
- What is Collection in Java?
- Java Collection Structure
- List Interface
- Queue Interface
- Set Interface
- Map Interface
Before starting this tutorial you must need to know what Collection in Java is and how to use it in programming. You also need to know the benefit of collection. A collection is like an array which stores collection of data but there is huge difference between them. An array size is fixed and it is defined while initialized array but the size of collection objects is dynamic and can be grow or shrink at run time. There is also various usage of Collection object that is mentioned below:
- Collection support more flexible data structure than array.
- The complexity of Collection framework is encapsulated and you are provided several methods and properties to work with them. For example for adding any new element there is add() method. So, it simplifies your code and gives better access to collection element than array.
- It reduces your development effort by providing rich set of classes.
- It gives you robust code quality because it is tested by several high qualified programmers.
Structure of Collection
You will learn all the classes and interfaces of collections in next few chapters. All the classes are explained with complete programming examples.
A list is an order collections or sequence of items. You can insert, delete or search items based on index position. It also allows duplicate entry in the list collections.
A Queue is based on First in First Out formulae. This collection is designed for holding elements prior to processing. Apart from basic collection operations it provides additional insertion, extraction and inspection operations.
Unlike List Interface Set doesn’t allow adding duplicate values in collection. It contains method only inherited from collections and prohibited duplicate entries.
A Map is an object that maps keys to values. It contains unique keys for all the elements and that elements can be accessed later using keys value. A map cannot contain duplicate keys.
Iterator and ListIterator both are used for traversing collections still there are some differences between Iterator and ListIterator. Iterator and listiterator is very useful and we will use it widely in collection programming.
It is only overview of collections framework. We have kept this topic short and useful but all the interfaces are explained in details in separate chapter. So, let’s start with List Interface