× Please, log in to give us a feedback. Click here to login

You must be logged to download. Click here to login


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation


MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Java Collections: Basic Concept and Implementation

In this article we will learn about the basic concepts of java collections and their implementation in java programming.

When we think about a collection, it is nothing more than a container. In java programming we can describe a collection as an object that holds multiple elements into a single unit. Collections are used to store data, retrieve, manipulate, and communicate aggregate data with certain methods provided.

Prior to collection concept in java, there were few classes like Vector, Dictionary, Properties, and Stack to hold and manipulate data. But all of these classes were not under a unified framework; For example the implementation of the Properties class was different from the Stack class. To work in an interoperable environment, the Collection framework was designed to allowed different types of collections to work in a mutual environment.

Standard definition of Collection framework “A collections framework is a unified architecture for representing and manipulating collections”.

A Collections Framework mainly contains the following 3 parts:

  1. Set of interfaces
  2. Concrete class implementation of the interfaces
  3. Standard utility methods and algorithms

Later in this article, we will try to understand above listed parts in detail.

Advantages of Java Collection Framework

When we use java collections in our program, we have been given opportunity to use one of the most integrated and useful API of java.

Some of its benefits are:

  1. Reduced programming effort: Collection framework provides a lot of data structures and easy implemented functions that certainly reduce the programming effort and give opportunity to programmer to concentrate on the important parts of program rather than on the low-level details required to make it work.
  2. Increase quality and speed: This Collections Framework provides high-performance, high-quality implementations of useful data structures and algorithms. Collections methods ensure the test operability and adopt certain algorithms that definitely speed up the execution of the program so that we have more time to devote to improving programs' quality and performance.
  3. Consistency: Many APIs naturally take collections on input and furnish them as output. In the past, each such API had a different implementation structure.

How are Collections Used

  • Java Collections stores object references, rather than objects themselves. So it become necessary to encapsulate the primitive types into objects using the wrapper classes hence primitive values cannot be stored in a collection directly.
  • As java collections store object references, they always stored the as type Object. So, when we retrieve an element from a collection type, we get an Object rather than the actual type of the collection stored in the database. So we would require to downcast it to the actual type while retrieving an element from a collection.

As now as we are aware of the importance of java collections, let’s discuss the important integrated parts of the collection framework that make them very useful for java programmers.

Core Collection Interfaces

Core collection interfaces are the foundation of the Java Collections Framework. Following are the core interfaces that define and allow collections to be manipulated independently of the details of their representation.

  • Collection:Collection interface comes first in the collection hierarchy. A collection represents a group of objects known as its elements. The Java platform doesn’t provide any direct implementations of this interface.
    The interface has methods to tell us how many elements are in the collection (size, isEmpty), to check whether a given object is in the collection (contains), to add and remove an element from the collection (add, remove), and to provide an iterator over the collection (iterator).
    Collection interface also provides operations (methods) that work on entire collection – containsAll, addAll, removeAll, retainAll, clear.
  • List:List is one of the most used collection type, the List interface extends the Collection interface and declares the behavior of a collection that stores a sequence of elements. List is an ordered collection and can contain duplicate elements. You can access any element from its index. However we can think list is like an array with dynamic length. ArrayList and LinkedList are implementation classes of List interface.
    Elements can be inserted or accessed by their position in the list, using a zero-based index.
  • Set:Set is a collection that cannot contain duplicate elements. This interface models mathematical set abstraction and is used to represent sets, such as the deck of cards.
    The Java platform contains three general-purpose Set implementations: HashSet, TreeSet, and LinkedHashSet. Set interface doesn’t allow random-access to an element in the Collection. You can use iterator or foreach loop to traverse the elements of a Set.
  • SortedSet:SortedSet is a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls. SortedSet is more generalize than Set and contains several useful methods that may be utilized in complex data algorithms implementation.
  • Map:A Map is an object that maps unique keys to values. A map cannot contain duplicate keys in its collection and each key can map to at most one value.
    The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap.
    The basic operations of Map are put, get, containsKey, containsValue, size, and isEmpty.
  • SortedMap:The SortedMap interface extends Map. It ensures that the entries are maintained in ascending key.
    The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap.
    The basic operations of SortedMap are put, get, containsKey, containsValue, size, and isEmpty.
  • Enumeration:The Enumeration interface defines the methods by which you can enumerate the elements in a collection of objects.

Core Collection Classes

As we had discussed about the core collection interfaces, we know that we need some concrete collection classes that implements Collection interfaces. Some of the classes provide full implementations that can be used as-is and others are abstract classes, providing skeletal implementations that are used as starting points for creating concrete collections.

  • LinkedList:LinkedList is a concrete class for List and Deque interfaceswe discussed earlier. This class Implements all optional list operations, and permits all elements (including null) in it.
  • All of the operations perform as could be expected for a doubly-linked list.
    Some operations that can perform on LinkedList class are: addAll(), addFirst(), set() , indexOf(), remove() etc.
  • ArrayList:ArrayList class is also concrete class for List interfaces implementation, in other words it can be treated as resizable-array implementation of the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.
    Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
  • HashSet:HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. Hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code.
  • HashMap:The HashMap class uses a hashtable to implement the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits null. This class makes no guarantees for the order of the map.
  • TreeMap:The TreeMap class implements the Map interface by using a tree. A TreeMap provides an efficient means of storing key/value pairs in sorted order, and allows rapid retrieval.

Collections API Algorithms

Java Collections Framework provides algorithm implementations that are commonly used such as sorting and searching. Most of these algorithms work on List but some of them are applicable for all kinds of collections.

Now let’s understand the practical implementation of some collection classes.

Listing 1.Storing and retrieving of a few Collection Types

  import java.util.*;
  public class CollectionsDemoExample {
   public static void main(String[] args) 
   List a1 = new ArrayList();
   System.out.println(" ArrayList Elements");
   System.out.print("\t" + a1);
   List l1 = new LinkedList();
   System.out.println(" LinkedList Elements");
   System.out.print("\t" + l1);
   Set s1 = new HashSet(); // or new TreeSet() will order the elements;
   System.out.println(" Set Elements");
   System.out.print("\t" + s1);
   Map m1 = new HashMap(); // or new TreeMap() will order based on keys
   m1.put("Windows", "98");
   m1.put("Win", "XP");
   m1.put("Beginner", "Java");
   m1.put("Tutorial", "Site");
   System.out.println(" Map Elements");
   System.out.print("\t" + m1);

Output Listing 1:

ArrayList Elements
 [Beginner, Java, tutorial]
 LinkedList Elements
 [Beginner, Java, tutorial]
 Set Elements
 [tutorial, Beginner, Java]
 Map Elements
 {Tutorial=Site, Windows=98, Win=XP, Beginner=Java}

Explanation Listing 1: Here we created a class “CollectionsDemoExample”. We used some concrete collection classes like ArrayList, LinkedList, HashSet, HashMap and performed some operations (add data) on these classes. As you can see ArrayList implements the List Interface, LinkedList implements the List Interface and HashMap implements the Map Interface. Also you can analyze the same structure among the collection classes.


In this tutorial we used the basic concepts of Collection interfaces and concrete classes. We also learnt how to use collection classes in java using a sample.

Computer system engineer who has 4 year of experience in the field our web-development ,FPGA, and java programming.

What did you think of this post?
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!

> More info about MrBool Credits
You must be logged to download.

Click here to login