Abc2 Java Collections
Abc2 Java Collections
Collection
• If we want to represent a group of objects as single entity then we should go for
collections.
• Collection framework:
It defines several classes and interfaces to represent a group of objects as a single
entity.
9(Nine) key interfaces of collection framework:
1. Collection
2. List
3. Set
4. SortedSet
5. NavigableSet
6. Queue
7. Map
8. SortedM ap
9. NavigableM ap
Collection interface ( Root Interface )
1. If we want to represent a group of "individual objects" as a single entity then we
should go for collection..
2. In general we can consider collection as root interface of entire collection
framework.
3. Collection interface defines the most common methods which can be applicable
for any collection object.
1. Enumeration
2. Iterator
3. ListIterator
1. Enumeration (java 1.0 V)
• We can use Enumeration to get objects one by one from the legacy
collection objects.
• We can create Enumeration object by using elements() method.
1. Both enumeration and Iterator are single direction cursors only. That is we can
always move only forward direction and we can't move to the backward direction.
2. While iterating by Iterator we can perform only read and remove operations and
we can't perform replacement and addition of new objects.
3. To overcome these limitations sun people introduced listIterator concept.
ListIterator (java 1.2 V)
1. ListIterator is the child interface of Iterator.
2. By using listIterator we can move either to the forward direction (or) to the
backward direction that is it is a bi-directional cursor.
3. While iterating by listIterator we can perform replacement and addition of new
objects in addition to read and remove operations
4. By using listIterator method we can create listIterator object.
public ListIterator listIterator();
ListIterator itr=l.listIterator(); // (l is any List object)
Methods of listiterator
1. public boolean hasNext();
2. public Object next(); forward
3. public int nextIndex();
4. public boolean hasPrevious();
5. public Object previous(); backward
6. public int previousIndex();
7. public void remove();
8. public void set(Object new);
9. public void add(Object new);
Enumeration Iterator and ListIterator ?
Property Enumeration Iterator ListIterator
Is it legacy ? Yes No No
It is applicable for? Only legacy classes Applicable for any Applicable for only list
collection object objects
moment Single direction Single direction Bi- direction
@Override
public int compareTo(Student other) {
// Compare based on age
return Integer.compare(this.age, other.age);
}
}
// Accessing values
System.out.println("Age of Bob: " + studentAges.get("Bob"));
• Usually Queue follows first in first out(FIFO) order but based on our requirement we
can implement our own order also.
• From 1.5v onwards LinkedList also implements Queue interface. LinkedList based
implementation of Queue always follows first in first out order.
• Assume we have to send sms for one lakh mobile numbers , before sending
messages we have to store all mobile numbers into Queue so that for the first
inserted number first message will be triggered(FIFO).
Methods of Queue interface
• boolean affer(Object o);
To add an object to the Queue.
• Object poll() ;
To remove and return head element of the Queue, if Queue is empty then we
will get null.
• Object remove();
To remove and return head element of the Queue. If Queue is empty then this
method raises Runtime Exception saying NoSuchElementException.
• Object peek();
To return head element of the Queue without removal, if Queue is empty this
method returns null.
• Object element();
It returns head element of the Queue and if Queue is empty then it will raise
Runtime Exception saying NoSuchElementException.
PriorityQueue
• PriorityQueue is a data structure to represent a group of individual objects prior to
processing according to some priority.
• The priority order can be either default natural sorting order (or) customized sorting order
specified by Comparator object.
• If we are depending on default natural sorting order then the objects must be
homogeneous and Comparable otherwise we will get ClassCastException.
• If we are defining our own customized sorting order by Comparator then the objects need
not be homogeneous and Comparable.
• Duplicate objects are not allowed.
• Insertion order is not preserved but all objects will be inserted according to some
Priority.
• Null is not allowed even as the 1st element for empty PriorityQueue.Otherwise we
will get the "NullPointerException".
Constructor of priorityQueue
• PriorityQueue q=new PriorityQueue();
Creates an empty PriorityQueue with default initial capacity 11 and default
natural sorting order.
• PriorityQueue q=new PriorityQueue(int initialcapacity,Comparator c);
• PriorityQueue q=new PriorityQueue(int initialcapacity);
• PriorityQueue q=new PriorityQueue(Collection c);
• PriorityQueue q=new PriorityQueue(SortedSet s);