If we want to represent a group of individual objects as a single entity then we should go for collection. In general collection interface is consider as root interface of collection framework but there is no concrete class which implements collection interface directly.
- The list is child interface if the collection
- If we want to represent a group of objects as a single entity such that duplicates are allowed and insertion order must be preserved
- Common methods in a list interface
=>void add(int index,Object o)
=>Object get(int index)
=>Object set(int index, new Object) — replace object present in that index and return old object
=>Object remove(int index)
- ArrayList is one of the implementation class of List interface and it is a growable array.
- Heterogenous object and null insertion are allowed.
- constructors for array list
- ArrayList is the best option if our frequent operation is retrieval operation, we should go for ArrayList if our frequent operation is insertion and deletion in the middle
=>ArrayList l=new ArrayList
=>ArrayList l=new ArrayList(int initial capacity) — creates a empty arraylist with specified capacity
=>ArrayList l=new ArrayList(Collection c) — creates ann araylist for given collection
- The underlying data structure is double LinkedList
- Duplicate and heterogeneous objects are allowed
- LinkedList is the best choice if our frequent operation is insertion and deletion
LinkedList l=new LinkedList();
LinkedList l=new LinkedList(Collection c); — creates a equivalent linked list for passed collection.
LinkedList specific methiods
void addFirst(Object o)
void addLast(Object o)
Object getFirst(Object o)
Object getLast(Object o)
- Vectors are very similar to ArrayList but every method present in the vector is synchronized
- At a time only one thread is allowed to operate on vector and hence it is a thread-safe and relative performance in low.
- Vector is introduced in 1.0 v it is legacy class.
a)Vector v=new Vector() — creates an empty object with default initial capacity 10, once vector reaches its max capacity then a new vector is created with current capacity*2.
b) Vector v=new Vector(int initial capacity)
c)Vector v=new Vector(int initial capacity, int incrementCapacity) — new vector is created when the vector is filled up to its increment capacity
d)Vector v=new Vector(Collection c)
It is the child class of vector, this class is designed for LIFO order
Constructor: Stack s=new Stack();
specific methods for stack:
Object push(Object o) —insert an object in the stack
Object pop() — remove and return top of the stack
Object peek() — return top of the stack without removal
boolean empty() — return true if stack is empty
Which List implementation class should be used for different operations:
a)Insertion at last index: Both LinkedList and ArrayList has same time complexity O(1) but LinkedList is preferred because in case of ArrayList first it will check if the array is full, if it is full then it will create a new array double size of the present array and copy all value, but LinkedList displace some pointers to do so, Therefore LinkedList is prefered.
b)Insertion value in the given index: Here also both of them has same time complexity O(n), but LinkedList is preferred because of the same reason above in point (a) of this section.
c)Search by value: Here also both have the same time complexity of O(n) but ArrayList‘s data structure is a growable array and array elements are stored in continuous memory location but linkedList’s underlying data structure is double LinkedList as nodes are created in random location Therefore ArrayList is preferred for this because iterating through continuous memory locations efficient than iterating through random memory location.
d)Get element by index: Here ArrayList is preferred because it implements random access interface with O(1) but in case of linkedList we need to iterate all element O(n).
e)Remove by value: For LinkedList and ArrayList we need to iterate through all nodes O(n) but in case of ArrayList we need to displace all elements after it but for LinkedList, we need to displace pointers. Therefore LinkedList is prefered.
d)Remove by index: ArrayList and linked list both can take O(n) but using random access ArrayList can reach to that index and after deletion, we need to displace all elements after that but in case of LinkedList, we just need to iterate and displace some pointers in the destination. so LinkedList is prefered as displacing all elements is not a good option in performance-wise.
The 3 cursours in java
- List Iterator
Enumeration is used to get object one by one from legacy collection Object
we can create enumeration object by using elements() method of vector class
eg: Vector v=new Vector();
public boolean hasMoreElements(); — if more elements are present in vector
public Object nextElements(); —get the next element
limitation of enumeration
a)we can apply this concept only for legacy classes ie: this is not a universal cursor
b)by using enumeration we can get only read access and we can not perform removal operation.
we can apply iterator concept for any type of collection objects and hence it is universal cursor. By using this we can perform both read and removal operation
we can create iterator object using iterator method of collection interface
Iterator itr=c.iterator() — where cis collection
- public boolean hasNext()
- public Object next()
- public void remove()
Limitations of iterator
- By using Iterator we can always move only towards the forward direction and we can’t move in backward direction
- By using iterator we can perform only read and remove operations and we can’t perform replacement and addition operation
By using list Iterator we can move forward as well as backward direction. We can also perform the replacement and addition of new objects.
we can create list Iterator by using ListIteratir method of list Interface.
eg: ListIterator itr=l.listIterator(); — -where l is list object
Limitation of listIterator
List Iterator can only be used only with list objects only.
Set is a child interface of collection if we want to represent a group of individual objects as a single entity, where duplicates are not allowed and insertion order not preserved.
- This is an implementation class of set interface
- Duplicate objects are not allowed and insertion order is not preserved
- null insertion is possible and Heterogenous object is allowed
- In HashSet duplicates, objects are not allowed, if we are trying to insert duplicate object then add() method return false.
- Here hashTable data structure in use, therefore, insertion, removal, lookup operations operation takes O(1).
HashSet h=new HashSet();
HashSet h=new HashSet(int initial capacity);
HashSet h=new HashSet(Collection c);
HashSet h=new HashSet(int initial capacity,float fillratio);
Its is child class of HashSet, it is exactly same as HashSet including constructor snd method but here insertion order is preserved.
It is a combination of LinkedList and HashTable, therefore insertion, removal, lookup operations operation takes O(1).
Is the child interface of the set, if we want to represent a group of the individual object according to some shorting order without duplicates then we should go for the shorted set.
As its implementing data structure is
a) Object first() — return the first element of the shorted set
b) Object last() — return the last element of the shorted set
c)SortedSet headSet(object obj) — return shorted set whose set is less than the object
d)ShortedSet tailSet(Object obj) — return shorted set whose elements are greater than equal to the object.
This is present in java.lang package and it contains only one method
public int compareTo(Object obj)
obj1.compareTo(obj2) — return negative value if obj1 come before obj2
return positive value if obj1 come after obj2
return 0 if obj1 and obj2 are equal
JVM internally use comparable interface compareTo() method for default shorting.
Comparator present in java. util package and it defines two methods
whenever we are implementing comparator interface, compulsory we should provide implementation only for compare method and we are not required to provide the implementation for the equals method because it is already available to our class from obj. class through inheritance.
compare(obj1,obj2) — return a negative value if obj1 come before obj2
— return positive value if obj1 come after obj2
— return 0 if obj1 and obj2 are equal
we can customize which element should come before or after by returning any positive value for obj 1 come before obj2 vice versa.
Comparable vs comparator
- For predefined comparable class-default natural sorting order already available, if we are not satisfied with that default natural sorting order then we can define our own shorting by using a comparator.
- For predefined non-comparable class (string buffer) default natural sorting order not available, we can define our own shorting using comparator.
Insertion order is not preserved and heterogeneous objects are not allowed. In TreeSet all objects are inserted based on some shorting order natural or customized order. Internally it uses Red-black tree therefore add, remove, next time complexity is O(log n) and to check if an object is present in tree will take O(1).
- Boolean offer(Object o) — to add an object into the queue
- Object peek() — to return head element of the queue, if the queue is empty then this method gives exception
- Object element() — To remove the head element of the queue, if the queue is empty then this method return null
- Object poll() — to remove and return the head element of the queue. If the queue is empty then this method return null.
- Object remove() — to remove and return the head element of the queue. If the queue is empty then this method raise runtime exception.
- If we want to represent a group of individual Objects, prior to processing according to some priority then we should go for priority Queue.
- The Priority can be either default natural sorting order or customized sorting order defined by the comparator.
- Insertion order is not preserved and it is based on some priority and duplicate objects are not allowed
- If we are depending on default sorting then compulsory elements should be homogenous and comparable.
- Null insertion is not allowed.
- offer() -O(log n),peak()-O(1),poll-O(log n),remove-O(n)
- Map is not a child interface of collection, If we want to represent a group of objects as key-value pairs then we should go for map.
- Both keys and values are objects only
- duplicate keys are not allowed but values can be duplicated
- Each key-value pair is called entry, hence map is considered as a collection of entry obj.
Object put(Object key, Object value)
void get(Object Key)
boolean containsKey(Object key)
object remove(Object Key) — remove the entry associated with specified key
A map is a group of Key value pair and each key-value pair is called an entry, Hence map is considered as a collection of entry objects.
- The underlying data Structure is hashTable
- Insertion order is not preserved and it is based on the hash code of keys
- Duplicate keys are not allowed but values can be duplicated
- Heterogenous Object is allowed for key and values
- Null is allowed for the key (only once) and null is allowed as a value for multiple time.
- get()-O(1),ContainKey()-O(1) as internally use hash table
HashMap m=new HashMap();
HashMap m=new HashMap(int initial capacity);
HashMap m=new HashMap(int initial capacity, float filled ratio);
HashMap m=new HashMap(Map m);
It is child class of hashmap, it is exactly same as hashmap(including methods and constructors) but here insertion order is preserved, and it is based on Linked list and hash table.
get()-O(1),ContainKey()-O(1) as internally use hash table and Linked list
It is exactly same as Hashmap except for the following difference, in the case of normal Hashmap JVM will use .equals method to identify duplicate Keys, which is meant for context comparison, but in case of Identity, HashMap JVM will use == operator to identify duplicate key which is meant for reference comparison.
Time complexity for method: get()-O(1),ContainKey()-O(1)
It is the child Interface of Map if we want to represent a group of Key-Value pairs according to some sorting order of keys, then we should go for a sorted map.
- Object firstKey()
- Object LastKey()
- SortedMap headMap(Object Key)
- SortedMap tailMap(Object Key)
In the case of hashMap keys are object and if it doesn't has any references then they are not eligible for garbage collection, but in the case of weak hashmap it is completely opposite.
time complexity for method: get()-O(1),ContainKey()-O(1)
This is the implementation class of a sorted map, the data structure used is a red-black tree. Insertion order is not preserved and it is based on some shorting order of keys and with its corresponding values.
Time complexity for method: get()-O(log n),ContainKey()-O(log n)