Collections in Java

Arrays are used in java to store several objects. The issue with array is that it does not support dynamic allocation. It has a fixed length which is not changeable once it is declared. Moreover, array is a very simple linear structure and many applications require more flexible and complex structure such as linked list, trees, maps and sets. Java collections gave us opportunity to overcome this situation and are widely used by programmers in their programs.

What is a collection framework?

Caore Java library provides an interface called collection framework which give mandates the common behaviors of all classes and flexible data structures such as ArrayList, LinkedList, Vector, HashSet, Stack, Hashtable, and HashMap. Internally it uses the arrays for the storage and hide the complexity by managing the dynamic size. The data stored in the collection is encrypted and accessible via predefined methods. For example if an application stores the information of a student, collection stores the information of many students.  Following are the key points that is been provided by collection interface.

  1. Obtain high performance
  2. Unified interface : allows different type of collections with common behavior and high level of interoperability
  3. Extend or adopt the collections easily
  4. Increase the software reuse ability

The Java Collection Framework package (java.util) contains:

  1. A set of interfaces
  2. Implementation classes
  3. Algorithms (such as sorting and searching)

Important Default Implementation in Collections

Some of the most important implementations are stack, queue, dequeue, list, and tree.  List interface has several implementation such as ArrayList and LinkedList classes.  Following is the example which elaborates how to use List interface in your code.

In java 8 lambda expression are incorporated with collections to simplify the operations. The example below shows how to create a collection of type list which is parametrized with to indicate compiler that only Strings are allowed in this collection. Later it uses a foreach loop from java and pass a method reference to print the list elements on the screen.

You may also sort the list using lambda expressions. The following example shows different ways to sort the list.

Removing a list member using lambda expression. Following example shows how to remove A from the list.

Bulk Operation in Collection Interface

Java collections provides methods to perform bulk operations such as clear the whole list, merge two collections into one and remove all from the collection. One can use these shorthand operations using these operation provided by Collection interface. Following code elaborates the functionality of the following methods:

addAll: Adds all the elements of a Collection to another Collection

containsAll: It returns true if target collection contains all elements in given collection

removeAll: Removes all the elements from target collection that are also in specified collection

retainAll: Retain only elements of target collection that are also in the specified collection

clear: Clears/removes all elements from a collection

More Useful Methods in Collections Class

Sort Method

Reverse Method

Copy Method

Copy all elements from source to target collection.

Shuffle Method

Swap Method

Swap element 0 and 1 from the given list.

No Responses

Leave a Reply

Your email address will not be published. Required fields are marked *