The collection present in Java is the framework for providing the architectures to store and manipulating the groups of objects. All the operations that are performed in the data like searching, sorting, manipulation, deletion, insertion and many others can also be performed by the Java Collections.
In Java, a collection is a group of objects. The Collections Framework contains a huge number of classes and interfaces that provide the functionality to manage collections. Collection interfaces provide highly efficient implementations that allow you to perform operations on collections. By using these implementations, you can take advantage of the standard functionality in Java without the need to write lengthy 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 the Collection Framework in Java?
Collection in Java represents the single unit of objects such as a group.Its some times called as container. Typically collections are representing the data items such as coins collection (All the coins are grouped together at one place) and mail box.
Collection framework always represents the unified architecture that are used for storing the objects. Collection Framework has some of the techniques such as
The Collection Interfaces
Collection is a generic interface that is implemented by all the collection classes. In this interface, all the fundamental methods required to work on collections are declared. The Collection interface is at the top of the hierarchy with several classes extending it. It extends the Iterable interface allowing the use of for-each loop to cycle through collection elements.
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:
Ability to access or add elements at specific positions in a list. The List interface uses an index starting at 0.
Ability to insert duplicate elements into a list.
Availability of methods inherited from the Collection interface and additional methods to manage lists.
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.
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.
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.
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 are:
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.