Collection Framework
The Collection in Java is a framework.
Java Collections can achieve all the operations that you
perform on a data such as searching, sorting, insertion, manipulation, and
deletion.
Java Collection framework provides many interfaces (List, Set,
Queue, Deque) and
classes (ArrayList, Vector, LinkedList, PriorityQueue,
HashSet, LinkedHashSet, TreeSet).
All the collection classes are available in “java.util”
(utility) package.
The collections are designed to store only objects that is
the collections cannot store
primitive type values.
Note: Collection framework provides these many interface/classes. Developer understands all features of each class and utilizes any one class for their requirement.
List Interface: ( Array List, Linked List, Vector, Stack)
List interface extends collection interface and It is
available in “java.util” package.
List is an ordered collection it maintains the insertion
order.
List provides control over the position where you can insert
the elements.
You can access elements by their index and search elements
in the list.
List allows you to add duplicate elements.
List allows you to have null elements.
List indexes start from 0, just like arrays
ArrayList:
Array List is maintained insertion order and you can add and
remove element in middle.
Java Array List class can contain duplicate elements.
Java collection framework was non-generic before JDK 1.5.
Since 1.5, it is generic.
You can’t create an ArrayList of primitive types like int, char etc.
Array List default initial capacity 10.
Array List is resized by 50% when it exceeds current default
size.
Manipulation with Array List is slow because if any element removed from the Array List all the elements are shifted in memory.
Java Array List class is non synchronized, you must
explicitly synchronize access to an Array List if multiple threads are goanna
modify it.
Array List is better for retrieving data.
Linked List:
Linked List class maintains insertion order.
Java LinkedList class can contain duplicate elements.
Manipulation with Linked List is faster than Array List because it internally uses doubly LinkedList, so no bit shifting required in memory. so entire memory will not effect.
Doubly Linked List − Items can be navigated forward and
backward way.
Java LinkedList class is non synchronized.
Vector:
In Java, both Array List and Vector implements the List
interface and provides the same functionalities. However, there exist some
differences between them.
The Vector class synchronizes each individual operation.
Array List saves memory compared with Vector. because Array List
& Vector default capacity is 10.
Array List is resized by 50% when it exceeds current default size. But Vector increase the capacity by 100%.
Stack:
The Stack class extends the Vector class.
In stack, elements are stored and accessed in LIFO manner last-in-first-out i.e., elements are added to the top of the stack and removed from the top of the stack.
Set Interface: ( Hash Set, Linked HashSet,TreeSet, Stack)
The set interface extends the Collection interface.
Set is an unordered collection it doesn’t maintains the
insertion order.
Set not allowed to add duplicate elements.
HashSet:
HashSet uses Hash table data structure.
HashSet contains unique elements only if we try to insert duplicates,
we won’t get compile time or runtime errors, add() method simply returns false
value.
HashSet doesn't maintain the insertion order, here, elements are inserted based on their hash code.
HashSet allows one null value.
The initial default capacity of HashSet is 16, and the load
factor is 0.75, here load factor means always load factory value should be 0-1.
suppose the elements of hash set moved to another hash table
that is double the size of the original once it is filled by 75%.
HashSet is not synchronized.
HashSet is the best approach for search operations.
LinkedHashSet:
LinkedHashSet similar to HashSet class. but it maintains the
insertion order.
LinkedHashSet contains unique elements.
LinkedHashSet uses a hash table & doubly linked list to
store & maintain the elements.
LinkedHashSet allows one null value.
LinkedHashSet is non-synchronized.
TreeSet:
Java TreeSet class contains unique elements only like HashSet, but java TreeSet class maintains ascending order.
Java TreeSet class access and retrieval times are quite
fast.
Java TreeSet class doesn't allow null element, throwing null
pointer exception.
TreeSet class is non synchronized.
Queue Interface:
The Queue interface extends collection interface.
Java Queue interface orders the element in FIFO (First-In-First-Out) manner.
In FIFO, first element is removed first, and last element is removed at last.
Deque Interface:
Deque interface extends Queue interface.
Deque interface is related to the double ended queue that supports addition or removal the elements from either end of the data structure.
It can either be used as Queue(FIFO) or as a Stack(LIFO).
Deque is acronym for double ended Queue.
ArrayDeque:
Unlike Queue, we can add or remove elements from both sides.
Null elements are not allowed in the ArrayDeque.
ArrayDeque class is faster than LinkedList and Stack.
ArrayDeque is not thread safe.
java.util.map
Map Interface: (HashMap,Linked HashMap,TreeMap)
The Java Map interface is not a part of the Collection
interface and it is available in java.util package.
Maps are great when we want to use key-value pairs such as a
dictionary.
Map contains unique keys, but you can have duplicate values.
Map contains values based on key, i.e., key and value pair.
Each key and value pair are known as an entry.
Map is useful for searching, update or delete elements on
the basis of a key.
HashMap and Linked Hash Map allow null keys and values, but
Tree Map doesn't allow any null key or value.
Map can't be traversed, so you need to convert it into Set
using keySet() or entrySet() method.
HashMap:
Java HashMap class implements the Map interface.
Java HashMap maintains no order.
Java HashMap contains values based on the key.
Java HashMap contains only unique keys If you try to insert
the duplicate key, it will replace the element of the corresponding key.
Java HashMap may have one null key and multiple null values.
It is easy to perform operations using the key index like
updating, deletion, etc.
Java HashMap is non synchronized.
The initial default capacity of Java HashMap class is 16
with a load factor of 0.75.
Linked HashMap:
Linked HashMap class extends HashMap class and implements
the Map interface.
The Linked HashMap Class is just like HashMap with an additional feature it maintains insertion order.
Tree Map:
Java Tree Map maintains ascending order
Java Tree Map contains only unique elements.
Java Tree Map contains values based on the key.
Java Tree Map cannot have a null key but can have multiple
null values.
Java Tree Map is non synchronized.
No comments:
Post a Comment