Collection Framework Implementations
A Collection represents a group of objects known as its elements. The Collection interface is used to pass around collections of objects where maximum generality is desired.
Architecture of collection Framework:
List Interface: A List is a ordered collection that can contain duplicate values.
ArrayList: ArrayList is said to be the best performing List implementation in normal conditions. In expendable array of value or objects.
Example: Copy Element of java ArrayList to Another java ArrayList.
Output: Before copy, Second ArrayList Contains : [One, Two, Three, Four, Five]
After copy, Second ArrayList Contains : [1, 2, 3, Four, Five]
LinkedList: LinkedList is a bit slower than ArrayList but it performs better in certain conditions.
Example: Copy Elements Of LinkedList To another LinkedList.
Output: Before copy, Second LinkedList Contains : [One, Two, Three, Four, Five]
After copy, Second LinkedList Contains : [1, 2, 3, Four, Five]
Vector: Vector is also a grow-able array of objects but unlike ArrayList. vector is thread safe in nature.
Example: Copy Elements Of Vector To another Vector.
Output: Before copy, Second Vector Contains : [One, Two, Three, Four, Five]
After copy, Second Vector Contains : [1, 2, 3, Four, Five]
Queue Interface: A Queue interface Provides implementation to hold elements prior to their prcessing.
Example: Add Elements Of Queue java.
Output: Before Add, one Queue Contains : [1, 2, 3]
After Add, Second Queue Contains : [1, 2, 3, manoj]
PriorityQueue: The PriorityQueue class is implemented as a priority heap.
Example: Add Elements Of PriorityQueue java.
Output: Before Add, One PriorityQueue Contains : [1, 2, 3]
After add, Second PriorityQueue Contains : [1, 2, 3, manoj]
Deque Interface: Deque is an interface you need to instantiate a concrete implementation of the interface in order to use it.
Example: Add Elements Of Deque java.
Output: Before Add, One DequeQueue Contains : [1, 2, 3]
After Add, Second DequeQueue Contains : [1, 2, 3, manoj]
ArrayDeque: ArrayDeque stores its elements internally in an array
Example: Add Elements Of ArrayDeque java.
Output: Before Add, One ArrayDeque Contains : [1, 2, 3]
After Add, Second ArrayDeque Contains : [1, 2, 3, manoj]
Set Interface: A Set is a Interface that does not contain duplicate values in java.
HashSet: HashSet is the best performing implementation of set interface. if stored its elements in a HashTable an does not guarantee of any type of ordering in iteration.
Example: Find Minimum element of Java HashSet Example.
Output: Element of Java HashSet is : [3298, 9237, 2374, 2473, 4298]
Minimum Element of Java HashSet is : 2374
TreeSet: TreeSet is a Little show than HashSet and it stored its elements in a red-block tree structure. TreeSet orders its elements on the basis of their values.
Example: Add element of Java TreeSet Example.
Output: TreeSet Output:[m, n]
Add elements output:[Java, m, n]
LinkedHashSet: LinkedHashSet is implemented as a Hashtable with a LinkedList running through it. it orders its elements on the basis of order in which they were inserted in the set.
Example: Add element of Java LinkedHashSet Example.
Output: LinkedHashSet Output:[m, n]
Add elements output:[m, n, Java]
Architecture of collection Framework:
List Interface: A List is a ordered collection that can contain duplicate values.
- ArrayList.
- LinkedList.
- Vector.
ArrayList: ArrayList is said to be the best performing List implementation in normal conditions. In expendable array of value or objects.
Example: Copy Element of java ArrayList to Another java ArrayList.
Output: Before copy, Second ArrayList Contains : [One, Two, Three, Four, Five]
After copy, Second ArrayList Contains : [1, 2, 3, Four, Five]
LinkedList: LinkedList is a bit slower than ArrayList but it performs better in certain conditions.
Example: Copy Elements Of LinkedList To another LinkedList.
Output: Before copy, Second LinkedList Contains : [One, Two, Three, Four, Five]
After copy, Second LinkedList Contains : [1, 2, 3, Four, Five]
Vector: Vector is also a grow-able array of objects but unlike ArrayList. vector is thread safe in nature.
Example: Copy Elements Of Vector To another Vector.
Output: Before copy, Second Vector Contains : [One, Two, Three, Four, Five]
After copy, Second Vector Contains : [1, 2, 3, Four, Five]
Queue Interface: A Queue interface Provides implementation to hold elements prior to their prcessing.
Example: Add Elements Of Queue java.
Output: Before Add, one Queue Contains : [1, 2, 3]
After Add, Second Queue Contains : [1, 2, 3, manoj]
PriorityQueue: The PriorityQueue class is implemented as a priority heap.
Example: Add Elements Of PriorityQueue java.
Output: Before Add, One PriorityQueue Contains : [1, 2, 3]
After add, Second PriorityQueue Contains : [1, 2, 3, manoj]
Deque Interface: Deque is an interface you need to instantiate a concrete implementation of the interface in order to use it.
Example: Add Elements Of Deque java.
Output: Before Add, One DequeQueue Contains : [1, 2, 3]
After Add, Second DequeQueue Contains : [1, 2, 3, manoj]
ArrayDeque: ArrayDeque stores its elements internally in an array
Example: Add Elements Of ArrayDeque java.
Output: Before Add, One ArrayDeque Contains : [1, 2, 3]
After Add, Second ArrayDeque Contains : [1, 2, 3, manoj]
Set Interface: A Set is a Interface that does not contain duplicate values in java.
- Hashset.
- TreeSet.
- LinkedHashSet.
HashSet: HashSet is the best performing implementation of set interface. if stored its elements in a HashTable an does not guarantee of any type of ordering in iteration.
Example: Find Minimum element of Java HashSet Example.
Output: Element of Java HashSet is : [3298, 9237, 2374, 2473, 4298]
Minimum Element of Java HashSet is : 2374
TreeSet: TreeSet is a Little show than HashSet and it stored its elements in a red-block tree structure. TreeSet orders its elements on the basis of their values.
Example: Add element of Java TreeSet Example.
Output: TreeSet Output:[m, n]
Add elements output:[Java, m, n]
LinkedHashSet: LinkedHashSet is implemented as a Hashtable with a LinkedList running through it. it orders its elements on the basis of order in which they were inserted in the set.
Example: Add element of Java LinkedHashSet Example.
Output: LinkedHashSet Output:[m, n]
Add elements output:[m, n, Java]