Java Collection
Java Collection
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
Class Name Data Structure Default Capacity Duplicate Value Null Value Insertion Order
Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the
Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The
synchronization is necessary for reliable communication between threads.
As we know Java has a feature, Multithreading, which is a process of running multiple threads simultaneously. When multiple
threads are working on the same data, and the value of our data is changing, that scenario is not thread-safe and we will get
inconsistent results. When a thread is already working on an object and preventing another thread on working on the same
object, this process is called Thread-Safety.
Difference between List and Set:-
List Set
List Allows Duplicate Value Set Does not allow duplicate value
List can save multiple null values in it Set can save only one null values in it
List Maintain Insertion Order Set doesnot maintain insertion order
Array List:-
• Array List is not synchronized.
• ArrayList is not thread safe.
• ArrayList is not legacy calsss.
• ArrayList is high on performance
• DataStructure is reliable.
ArrayList li = new ArrayList();
Linked List:-
• LinkedList implements List interface.
• LinkedList is better for manipulating data.
• LinkedList internally uses a doubly linked list to store the elements.
• LinkedList is not synchronized
LinkedList ll = new LinkedList<>();
Vectors:-
• Vectors is synchronized.
• Vector is thread safe.
• Vector is legacy class.
• Vector is slower than Array list.
• Data structure is doubling.
Vector<String> v =new Vector<>();
Priority Queue:-
• Priority Que impliments Queue interface.
• PriorityQueue doesn’t permit null.
• Element in this queue is associated with a priority value.
PriorityQueue<String> q =new PriorityQueue();
Hash Set:-
• HashSet stores the elements by using a mechanism called hashing.
• HashSet contains unique elements only.
• HashSet class is non synchronized.
• HashSet allows null value.
• HashSet doesn't maintain the insertion order. Here, elements are inserted on the basis of their hashcode.
HashSet<String> h =new HashSet<String>();
HashMap:-
•Java HashMap contains values based on the key.
•Java HashMap contains only unique keys.
•Java HashMap may have one null key and multiple null values.
•Java HashMap is non synchronized.
•Java HashMap maintains no order.
HashTable:-
• Java Hashtable contains values based on the key.
• Java Hashtable contains only unique keys.
• Java Hashtable does not accept any null key or value.
• Java Hashtable is synchronized.
• Java Hashtable maintains no order.