Java Collection Framework
Hey Shouters!! Today we have come up with the Java Collection Framework. It is based on some concepts rather than programming. Read these points carefully.
What is a Java Collection Framework?
A Java collection framework provides an architecture to store and manipulate a group of objects.
Interfaces, Classes and Algorithms are included in the Java collection framework.
One of Java's most powerful subsystem contained by the "java.util" package is the Collection Framework.
The developers can access prepackaged data structures as well as algorithms to manipulate data from the Java collection framework.
Add objects, Remove objects, Search for an object, Retrieve/get object, Iterate through the collection framework. These activities can be performed within Java Collection framework.
.
Java Collection Framework Hierarchy :
In this above image, blue part refers to the different interfaces and the yellow part defines the classes.
.
Java's Collection Framework has 4 basic parts i.e. Lists, Sets, Maps, Queues.
A short note about these are given below :
Lists:
List is an interface extends Collection(capital 'C' denotes java.util.Collection ) interface.
Values of lists can be duplicated.
ArrayList, LinkedList, and Vector are classes implementing List interface.
.
Sets:
The Set is an interface extends the Collection interface.
Values in Sets cannot be duplicated.
Three concrete classes of Set are HashSet, LinkedHashSet, and TreeSet.
.
Maps:
A container that stores the elements along with the keys is called a Map.
here, Keys are like indexes. In List, the indexes are integers. In Map, the Keys can be any objects.
Keys cannot be duplicated.
HashMap, HashTable, TreeMap, and LinkedHashMap are classes implementing Map interface.
.
Queues:
A queue is a FIFO data structure where insertion takes place at the end and deletion takes place at the front.
In the Priority Queue, The element with the highest priority is removed first.
.
The collection comes with some flavors as sorted, unsorted, ordered, and unordered.
Let's walk through it.
Ordered:
Iterate comes here with a specific order (not randomly).
You can iterate through the collection in a specific order.
.
Sorted:
Some Sorting rules or rules applied to the collection of objects. So, The order of objects in the collection is determined according to some rule or rules, known as the sorting order.
Sorting order means what "position" it was added at.
Unsorted and unordered are totally different from sorted and ordered.
.
ITERATOR INTERFACE:
Obtaining and removing elements from a collection of objects Iterator plays a great role.
ListIterator extends Iterator to allow bidirectional traversal of a list.
There are two important Iterator methods:
- boolean hasNext(): It is used to get a True value when there is at least one more element in the collection being traversed.
Note: Invoking hasNext() does not move you to the next element of the collection.
- object next(): This method is used to return the next object in the collection.
Note: It moves forward to the element after the element just returned.
.
COMPARATOR INTERFACE :
Using any number of different ways you can sort a given collection by using the comparator interface.
Also you can use it to sort instances of any class- even classes you can't modify- unlike the comparable interface, which forces you to change the class whose instance you want to sort.
The Comparator interface is also very easy to implement, having only one method compare().
The Comparator.compare() method returns an int.
Syntax: int compare(objOne, objTwo)
Compare method returns:
- negative if objOne < objTwo
- zero if objOne == objTwo
- Positive if objOne > objTwo
.
Summary :
Below table summarizes the discussion on Collection framework.
Collection Class Name | Ordered | Sorted | |
Hashtable | No | No | |
Map | HashMap | No | No |
TreeMap | Sorted | By natural order or custom order | |
LinkedHashMap | By insertion order or last access order | No |
Collection Class Name | Ordered | Sorted | |
HashSet | No | No | |
Set | TreeSet | Sorted | By natural order or custom order |
LinkedHashSet | By insertion order | No |
Collection Class Name | Ordered | Sorted | |
ArrayList | Indexed | No | |
List | Vector | Indexed | No |
LinkedList | Indexed | No |
Collection Class Name | Ordered | Sorted | |
Queue | Priority Queue | Sorted | By to-do order |
If you have any doubt regarding any question. Feel Free to comment below and if you to add any questions. Write your suggestion below in the comment box.
Comments
Post a Comment