Collections in java are set of Java classes that assist the objects to group them and manage them. Java Collections is a group of individual items in a single unit. Collections in java are like containers that merge multiple items into a single unit. For example a bundle of sticks, a list of employee names, etc. The two principal root interfaces of Java collection classes are Collection interface (java.util.Collection) and Map interface (java.util.Map).
Collection Framework in Java
A collection Framework in Java is a unified architecture that represents a collection of interfaces and classes. It helps in storing and processing the data efficiently.
This framework has several useful classes that have a number of useful functions that make a programmer’s task easy. The Collection Frameworks represent and manipulate Collections in Java in a standard way.
Need for Collection Framework in Java
It increases the efficiency of the fundamental collections like dynamic arrays, trees, linked lists, and hashtables, etc.
Let the different types of collections work in an identical manner along with a higher degree of compatibility.
Easily extend and/or adapt a collection.
Remove the need for writing the code to implement the data structures and algorithms manually.
Make our code much more efficient as the Collections Framework is highly optimized.
Make our data unique we can use the Set interface provided by the Collections Framework.
We can use the Map interface to store data in key/value pairs.
Enable the functionality of resizable arrays, we can use the ArrayList class.
Composition of Collection Framework in Java
All collections frameworks in Java include the following:
2) Implementation or Classes
Java Collection Framework consists of interfaces which are abstract data types that represent collections. With Interfaces, we can manipulate the collections irrespective of the details of their representation. All the interfaces of the collections framework reside in java.util package.
In object-oriented languages, interfaces normally represent a hierarchy. The root or top-level interface of the Collection Framework is java.util.Collection. It contains some important methods such as add (), size (), remove (), clear (), iterator () that every Collection class must implement.
Some other important interfaces are java.util.List, java.util.Queue, java.util.Set, and java.util.Map. The only interface that does not inherit the Collection interface is the Map interface but it is the part of the Collections framework.
The three main interfaces in Java are:
Indexing is not present. (Un-Ordered Collection).
Duplicate Objects are not allowed. (Unique Collection).
Null value is allowed.
EnumSet: Enumset particular class to work with enum types.
HashSet: HashSet keeps an unordered list of components (Arrange is eccentric).
LinkedHashSet: LinkedHashSet keeps requested rundown of components.
TreeSet: Treeset makes sure that there are no duplicates.
SortedSet: Sortedset provides ordering on its elements.
Indexing is present. (Ordered Collection).
Indexing starts from “0”. (Auto Indexing).
Duplicate Objects are allowed. (Unique Collection).
Null values are allowed.
ArrayList: Keeps an unordered list of components utilizing exhibit.
LinkedList: Keeps requested list of components utilizing doubly- connected rundown.
Vector: For the most part, the same as ArrayList, however, it is string saf
Data will be stored in key -Value pair.
1) First Name=Akshay, Last Name=Kumar, Mobile No=+91-9876543210, pin code=40001, Nick Name=Akki
2) Roll No and First Name-: 1=Akshay, 2=Salman, 3= Katrina, 4=Ranveer
Key Should be unique and value can duplicate.
Key can be null and multiple Values can be duplicate.
HashMap: Keeps unordered rundown of list utilizing exhibit
LinkedHashMap: Keeps requested list of components utilizing doubly- connected rundown.
TreeMap: Keeps requested list of components utilizing RBT. Components are requested by regular request or by a custom comparator.
Hashtable: Keeps an unordered list of components as HashMap, however, it is synchronized. This class is outdated.
EnumMap: Keeps ordered collection and are maintained in natural order.
Properties: It is subclass of HashTable. Provides methods to read and store data in properties file
2) Implementation or Classes
Java Collections framework provides implementation classes for collections which are the concrete implementations of the collection interfaces. In short, these classes are reusable data structures.
We can use them again and again to create different types of collections in Java code. Some important classes of collection framework are ArrayList, LinkedList, HashMap, TreeMap, HashSet, TreeSet.
These classes are more than enough to solve most of our requirements in programming, but if we still need some special collection class which we can extend to create our customized collection classes.
ArrayList: This class extends the AbstractList class and implements a dynamic array.
LinkedList: This class implements a linked list by and extends the AbstractSequentialList class.
HashSet: This class extends the AbstractSet class to work with a hash table.
HashMap: This class extends the AbstractMap class to use a hash table.
LinkedHashSet: This class extends the HashSet class and allows iterations in insertion-order.
LinkedHashMap: This class extends the HashMap class and allows iterations in insertion-order.
TreeSet: This class extends the AbstractSet class and implements the Set stored in a tree.
TreeMap: This class extends the AbstractMap class to use a tree.
An algorithm refers to the methods that perform useful computing operations, such as searching, sorting and shuffling on objects that implement collection interfaces.
The algorithms are polymorphic: that is, we can use the same method on several different implementations of the appropriate Java collection interface. We define these algorithms as static methods within the Collections class.
Iterator Interface in Java
An Iterator is an object that can be used to loop through collections, like ArrayList and HashSet. It is called an “iterator” because “iterating” is the technical term for looping.
To use an Iterator, we must import it from the java.util package.
The Methods Declared by Iterator
1) hasNext ()
Returns true if there are more elements. Otherwise, returns false.
2) next ()
Returns the next element. Throws NoSuchElementException if there is not a next element.
3) remove ()
Removes the current element. Throws IllegalStateException if an attempt is made to call remove () that is not preceded by a call to next ().
The Methods Declared by ListIterator
1) add (Object obj)
Inserts obj into the list in front of the element that will be returned by the next call to next ().
2) next ()
Returns the next element. A NoSuchElementException is thrown if there is not a next element.
3) hasNext ()
Returns true if there is a next element. Otherwise, returns false.
4) nextIndex ()
Returns the index of the next element. If there is not a next element, returns the size of the list.
5) previous ()
Returns the previous element. A NoSuchElementException is thrown if there is not a previous element.
6) hasPrevious ()
Returns true if there is a previous element. Otherwise, returns false.
7) previousIndex ()
Returns the index of the previous element. If there is not a previous element, returns -1.
8) remove ()
Removes the current element from the list. An IllegalStateException is thrown if remove () is called before next () or previous () is invoked.
9) set (Object obj)
Assigns obj to the current element. This is the element last returned by a call to either next () or previous ().
Difference Between Iterator And ListIterator
1) Iterator is used for traversing List and Set both.
We can use ListIterator to traverse List only, we cannot traverse Set using ListIterator.
2) We can traverse in only forward direction using Iterator.
Using ListIterator, we can traverse a List in both the directions (forward and backward).
3) We cannot obtain indexes while using Iterator
We can obtain indexes at any point of time while traversing a list using ListIterator. The methods nextIndex () and previousIndex () are used for this purpose.
4) We cannot add element to collection while traversing it using Iterator, it throws ConcurrentModificationException when you try to do it.
We can add element at any point of time while traversing a list using ListIterator.
5) We cannot replace the existing element value when using Iterator.
By using set (Object obj) method of ListIterator we can replace the last element returned by next () or previous () methods.