Monday , September 25 2017
Home / java / Collection Framework & Collection Interfaces in Java

Collection Framework & Collection Interfaces in Java

In this tutorials, we are going to discuss what is Java Collection Framework and what are the key interfaces of Java Collection Framework.

Collection Framework

A collection present in Java is a framework for providing the architectures to store and manipulating the groups of objects.

All the operations that are performed on data like searching, sorting, manipulation, deletion, insertion and many others can also be performed by the Java Collections.

Collection interfaces provide highly efficient implementations that allows you to perform operations on collections. By using these implementations, you can take advantage of standard functionality in Java without need to write lengthy of code.

  • Framework will give high performance.
  • Implementations of the highly efficient fundamental collections such as dynamic Arrays, Trees, LinkedLists and Hashtable.
  • Framework allows many different types of collections with high degree interoperability.
  • Extending and adapting the Framework collection is easy.

What is Collection Framework in Java?

Collection in Java represents the single unit of objects such as a group. It some times called as a container. Typically collections are representing the data items such as coins collection (All the coins are grouped together at one place) and mail box.

The Collections Framework contains a huge number of classes and interfaces that provides the functionality to manage collections.

Collection framework always represents a unified architecture that are used for storing the objects. Collection Framework has some of the techniques such as

  1. Interfaces
  2. implementations

What is Collection Interface ?

Collection is a generic interface that is implemented by all the collection classes. It provides all fundamental methods required to work on collections.  Below is the basic collection framework interface hierarchy.

Collection

Methods in the Collection Interface

The most commonly used methods in the Collection interface are:

  • int size(): Returns the number of elements in a collection.

  • boolean isEmpty(): Identifies whether a collection is empty. Returns true if the collection does not contain any element or else returns false.

  • boolean contains(Object element): Checks whether the specified element is a part of the collection. Returns true if the element is found or else returns false.

  • boolean add(E element): Adds the specified element to the collection. The type of element to be added must be compatible with the type of the collection. Returns true if element is successfully added to the collection. Returns false if the element is already found in the collection and the collection does not allow duplicates.

  • boolean remove(Object element): Removes one occurrence of the specified element from the collection. Returns true if the element is successfully removed or else returns false.

  • Iterator<E> iterator(): Provides an iterator for the collection.

In addition, there are methods that perform bulk operations on the entire collection, such as addAll, removeAll, containsAll, retainAll, and clear(). The equals() method is another important method that is used to compare two collections.

There is a conversion constructor in the Collection interface that allows new collections to be initialized with the contents of another collection.

Example: List<int> lst1 = new ArrayList<int> c;

where c is another collection that contains elements of int type.

The List Interface

The List interface provides the functionality to store and manage a sequence of items. It extends the Collection interface. Some of the significant features of the List interface include:

  •  The List interface uses an index starting at 0.

  • Ability to access(read) or add(insert) elements at specific positions in a list.
  • Ability to insert duplicate elements into a list.

  • Availability of methods inherited from the Collection interface and additional methods to manage lists.

List

Methods in the List Interface

The most commonly used methods in the List interface are:

  • void add(int index, E obj):Inserts the object obj at the location specified by index. If any element is already present at that location, the list elements are shifted.

  • E get(int index): Returns the object at the location specified by index.

  • E set(int index, E obj): Assigns the object obj to the specified location and returns the old object at that location.

  • int indexOf(Object obj): Identifies the fist occurrence of obj in the collection and returns its location. If the element obj is not found, -1 is returned.

The Set Interface

The Set interface provides the functionality to store and manage a set of elements that does not allow duplicates. It is a generic interface that extends the Collection interface and does not provide any additional methods. Some of the key characteristics of the Set interface include:

  • Capability to restrict duplicate elements. The add() method returns false if an attempt is made to insert a duplicate element.

  • Ability to compare the contents of two sets accurately. Because of the restriction on duplicates, when two set instances are compared using the equals() method, a true value is returned only if all the elements are equal.

Set

The SortedSet interface extends the Set interface and adds methods to manage a set of elements sorted in ascending order. The NavigableSet interface extends SortedSet and provides access to elements by matching one or more specific values.

The Queue Interface

The Queue interface extends the Collection interface to provide an implementation of a queue. In a queue, elements can be removed only from the head.

Queue

The Queue interface provides the functionality to add, remove, access, and examine queue elements. Typically, queues implement a first-in first out behavior. The Dequeue interface extends the Queue interface to support double-ended queues.

The Map Interface

The Map interface is a generic interface that provides a way to store key/value pairs. A map is an object that maps keys to values. Keys are unique and are used to identify values.

Map

Some of the important methods of the Map interface:

  • V get (Object k): Returns the value associated with the key k.

  • V put(K k, V v): Stores a value v to the map with the key k.

  • boolean containsKey(Object k):Searches for the key k and returns true if found.

  • boolean containsValue(Object v):Searches for the value v and returns true if found.

The other interfaces that support maps in Java :

  • Map.Entry: An inner class of Map that supports key/value pairs in a map.

  • SortedMap: An interface that extends the Map interface. A SortedMap instance maintains keys in ascending order.

  • NavigableMap: An interface that extends the SortedMap interface and handles retrieval by searching for the closest match.

Happy Learning 🙂

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Recommended

Binary Literals Java 7 Example Tutorials

Binary literals is one of the new feature added in Java 7. We already know …

Leave a Reply

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