Collection Programs in Java
Collection Programs in Java
Collection in Java provides a way to manipulate or store a group of objects. Each object in a collection is called element. Collection programs in Java mainly deals with the Java collection framework that provides various interfaces, classes that is used to implement different types of data structures and algorithms. For example, the implementation of doubly linked list is done by the class LinkedList of the collection framework.
Interfaces of Collection Framework
There are various interfaces that are integral part of the Collection Framework. Some of the interfaces that form the core of the Collection Framework are:
1) Iterable Interface: It is the root interface of collection framework hierarchy. This interface provides iterators for the collection elements. These iterators are very handy when traversal of elements is required.
2) Collection Interface: It extends the Iterable interface. Other interfaces, List, Set, and Queue extends the collection interface. Collection interface renders all the basic methods that are required by a collection element. In collection framework, no class directly implements this interface. This framework provides the foundation upon which the collection framework relies a lot.
3) List Interface: It is used to provide elements in ordered manner. Using this interface, one can add, remove, and update elements from the list. Elements can be inserted or accessed by their position in the list. Using List interface, it is possible to create dynamic lists, i.e., those lists whose size can be update during run-time. LinkedList, ArrayList, Vector, Stack are the classes that implement the List interface.
4) Set Interface: Set interface is similar to sets that we have learnt in Mathematics. Similar to mathematics, duplicate elements are not allowed in Set interface. TreeSet and LinkedHashSet are the classes that implement this interface.
5) Queue Interface: The queue interface provides the features of queue data structure. This means, elements of the classes that implement Queue Interface, are stored in First In First Out (FIFO) manner. Classes that implement this interface are ArrayDeque, PriorityQueue.
6) Map Interface: It does not extend the Iterable interface. This interface is another root of the collection framework. Objects of the class implementing the Map interface always store association between keys and values, i.e. it maintains key/ value pair. Note that the key, in the key/ value pair, has to be unique. However, duplicates values are allowed. TreeMap and HashMap are the classes that provide the implementation of the Map interface.
Methods of the Collection Interface
The Collection Interface provides core methods that all collections will have.
Methods | Description | |
public boolean add(Object o) | for adding an element | |
public static boolean addAll(Collection c) | Add the collection c to the given collection | |
public boolean remove(Object o) | remove the object o. | |
public boolean removeAll(Collection c) | Remove the collection C from the given collection | |
public boolean retainAll(Collection c) | All the elements of the collection c get retained, while others get deleted. | |
public int size() | returns size of the collection | |
public void clear() | remove all elements from the collection | |
public boolean contains(Object o) | Checks if the object o is present in the collection or not. | |
public boolean containsAll(Collection c) | Checks whether the collection c is present in the collection or not. | |
public boolean isEmpty() | Checks if the collection is empty or not. | |
public Iterator iterator() | Returns an iterator for the collection. | |
public Object[] to array | Converts the collection into array. | |
Object[] toArray(Object arr[]) | Returns an array whose type matches with the array arr. | |
public int hashCode() | for returning the hash code number of the collection. | |
public boolean equals(Object o) | compares the object o with the object that invokes this method. | |
Note: The last two methods are of the Object class.
Significance of the Collection Framework
1) This framework provides various algorithm and data structures that can be used directly in the code. Thus, we do not need to implement those data structures or algorithms manually. For example, for storing unique element only set interface can be use. For storing elements in ordered way, array list or linked list can be used.
2) Collection framework is highly optimized. Thus, the code written using this framework is highly efficient.
Let’s see an example of Collection.
CollectionExample.java
import java.util.*; class CollectionExample.java { public static void main(String args[]){ ArrayList<String> list=new ArrayList<String>();//Creating arraylist list.add("Norway");//Adding object in arraylist list.add("Atlanta"); list.add("Czech Republic"); list.add("Santa Clara"); //Traversing list through Iterator Iterator itr=list.iterator(); while(itr.hasNext()){ System.out.println(itr.next()); } } }
Output:
Norway Atlanta Czech Republic Santa Calara