ArrayList: Arraylist is Dynamic Array also called as Resizable Array.
Array can shrink and Expand
Automatically when we will add or remove elements.
- ArrayList contains Duplicate
- ArrayList maintains the insertion order
- ArrayList is not a Synchronized Class.
- ArrayList is good for sorting and Accessing the element.
- Random Access
- ArrayList manuplation is slow because lot of bit shifting is required when we adding and
removing an elements
LinkedList:
Linkedlist contains duplicate elements
Linkedlist maintain insertion order
LinkedList is not synchronized
Manuplation is faster in linkedlist
LinkedList implements Queue and List so it is having the property of both
Vector: is a legacy class
It is synchronized class
It will maintain the duplicate element and insertion order
It increases dynamically by the size
Generics In Java: (Java 5 Feature)
Generics has the feature of the Type safety
Difference Between List and Set?
List maintains duplicate and insertion order
Set doesn’t contain any duplicate.
Set: duplicate are not allowed
HashSet:
- No duplicate
- Insertion order is not going to maintain.
LinkedHashSet
- Maintains insertion order
- No duplicate allowed
TreeSet: - No duplicate
- It will give us the sorted data that in Ascending order
Why collection is not under the collection Hierarchy ?
Map uses key and value pair
Where as rest of the collection class uses only one value.
HashMap:
- It stores key and value
- It is not going to maintain insertion order
- Duplicate key not allowed
- One null key is allowed
- Many null values can be allowed
- HashMap is not Synchronized class
Internal work of the HashMap;
LinkedHashMap: it is same as hashMap only the difference is it LinkedHashMap maintain the instertion
order.
TreeMap: no null keys are allowed
It will sort the map in asceding order
HashMap VS HashTable
Hashtable is a legacy class introduced in java 1.0
Hashtable is synchronized class
Hashtable doesn’t allow any null key and null value
Hashmap is not synchronized
ConcurrentHashMap: is Synchronized map
Internal work of the ConcurrentHashmap:
ConcureentHashmap divides the map into the segment and put the lock on one of the segment not the
whole map.
The locking Mechanism is known as segment locking or Bucket Locking.
No other thread can access the Locked Segment . But they can access the rest of the map and can only
perform the read operation.
ConcurrentHashMap vs HashTable
ConcurrentHashMap vs Synchronized HashMap(HashMap is not synchronized by default but if we use
Collections.SynchronizedMap(pass hmap object).
Fail Fast: while doing the iteration in the collection if it throws the ConcurrentModificationException
that is called fail fast
Fail Safe: while doing any modification at the iteration time will not get any
ConcurrentModificationexception that class is fail safe class
e.g concurrentHashMap and CopyOnWriteArrayList.
Comparable Vs Comparator
Comparable : it provides a single sorting Sequence. E can sort the object based on the single element of
the class.
Using comparable interface we need to override the compareTo(Object obj) method.
Comparator interface: it provides the Multiple Sorting Sequence which means it will do the sorting
based on the Multiple elements of the class.
We have to override the compare (Object obj1, Object 2)
HashCode and Equals of Object:
WithOut Overriding of Hashcode and Equals Method of Object Class:
HashCode of Object class will return the integer Number which is different for every object
Equals Method: in object class is checking the reference .it works like == in string
Why we need to Override HashCode and Equals Method:
If we are overriding the hashcode it will first check it there are two objects same it will return the same
hashcode for that two objects.
And equals() method will compare the value of the object and return true if both are same else return
false.
After overriding the equals into the class it will compare the value of the object not the reference.