About Collections Framework in Java
Collections Framework
Collections Framework in Java is a collection of classes and interfaces that helps us to store and process the data efficiently. This Framework has several useful classes that make programmer’s tasks easy.
Need of Collections Framework
Before the Collections Framework was introduced, there were Arrays, Vectors, and Hashtables to work on objects. Though the main aim of these is the same they were defined independently and had no correlation among them. So, the users had to remember all methods, syntaxes, and constructors present in every class.
Collections Framework Hierarchy
Collection Types
- ArrayList
- LinkedList
- Vector
- HashSet
- LinkedHashSet
- TreeSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
ArrayList
ArrayList class implements List interface and it is implemented over Array data structure. ArrayList has an advantage over an array because it offers more flexibility and functionality.
Why ArrayList instead of Array?
Array is more like static type data structure, we can not increase the size of it if we want to add more items. Similarly, if we had deleted items in an array the memory consumed by that array is the same, it won’t shrink. ArrayList is a dynamic data structure, it can grow and shrink accordingly. It also maintains insertion order. Above all, it also had many predefined methods like add, remove, size, sort, etc that can make programmer’s task easy.
LinkedList
LinkedList is a linear data structure similar to ArrayList. LinkedList does not occupy the contiguous memory locations as Array. The data in LinkedList is connected with pointers. It has two parts data and next, data to store element and next is to store the address of the beside node.
LinkedList in java is Doubly LinkedList which can store the addresses of the previous node and the next node.
When to use ArrayList and LinkedList in java?
It depends on our requirements. If there is a need for frequent search operations choose ArrayList, If there is a need for frequent insertion, deletion operations choose LinkedList.
While inserting an element in Array, the element had to pass along all the previous elements. Similarly for deletion also, if an element is deleted in ArrayList all its elements next to it had to move to before the deleted element’s index. So, the time complexity for insertion and deletion in ArrayList is O(n).
In LinkedList, while inserting an element just the pointer will refer to the new element and while deleting an element, the pointer referencing to the element will refer to null (if deleted one is the last element) or to the next element (if deleted one in the middle). So, the time complexity for insertion and deletion in LinkedList is O(1).
When it comes to searching, ArrayList is better than LinkedList due to its indexes.
Vector
Vector implements the List interface. Vector is similar to an array that maintains insertion order.
Vector is synchronized means no two threads do not perform operations on it concurrently. So it does not perform insertion, deletion, updating, and searching effectively.
HashSet
HashSet implements the Set Interface. It is not synchronized. It does not maintain insertion. It does not allow duplicates. If we add a duplicate element, it would be overridden. It allows null values. If we add more than one null value, it still takes only one null value.
TreeSet
TreeSet implements the Set Interface. It is similar to HashSet. It maintains ascending order of values that are inserted into TreeSet. Since it is implementing a Set interface, it does not allow duplicates.
It is also not synchronized like most of the other classes.
LinkedHashSet
LinkedHashSet is also an implementation of the Set Interface. It is similar to HashSet and TreeSet except it maintains insertion order.
It is also not synchronized like most of the other classes.
HashMap
HashMap is an implementation of the Map interface. HashMap stores the data in the form of a Key, Value pair. It does not accept duplicate keys but it accepts duplicate values. It is unordered and accepts null keys and null values.
TreeMap
TreeMap is an implementation of the Map interface similar to HashMap except it maintains sorted order of keys. It is also not synchronized so not useful for thread-safe operations until we synchronize it.
LinkedHashMap
LinkedHashMap is a linked list implementation of the Map interface. It maintains insertion order. It is implemented with the doubly-linked list. It is not synchronized and it can accept one null key and multiple null values.
HashTable
HashTable class implements a Hash table that maps keys and values. Any non-null object can be used as keys and values in HashTable. It is synchronized, unlike all other classes.
Resources
THANK YOU! HAVE A NICE DAY!