Collection Framework
Collection Framework
java.util.Collection
Collection Framework
• A framework is a set of classes and interfaces which provide a
ready-made architecture
• Before jdk 1.2 the standard methods for grouping objects
• Arrays
• Vectors
• Hastables
• No common interface
• Difficult to remember different methods, syntax and constructors
• Consistent API
• Improved program performance
• Reduced programming efforts
Example Program
import java.io.*;
import java.util.*;
class CollectionDemo {
}
}
Hierarchy of Collection Framework
Methods in Collection interface
Method Description
add(Object) This method is used to add an object to the collection.
addAll(Collection c) This method adds all the elements in the given collection to this collection.
clear() This method removes all of the elements from this collection.
contains(Object o) This method returns true if the collection contains the specified element.
containsAll(Collection c) This method returns true if the collection contains all of the elements in the given collection.
equals(Object o) This method compares the specified object with this collection for equality.
hashCode() This method is used to return the hash code value for this collection.
isEmpty() This method returns true if this collection contains no elements.
iterator() This method returns an iterator over the elements in this collection.
max() This method is used to return the maximum value present in the collection.
parallelStream() This method returns a parallel Stream with this collection as its source.
This method is used to remove the given object from the collection. If there are duplicate
remove(Object o) values, then this method removes the first occurrence of the object.
This method is used to remove all the objects mentioned in the given collection from the
removeAll(Collection c) collection.
This method is used to remove all the elements of this collection that satisfy the
removeIf(Predicate filter) given predicate.
This method is used to retain only the elements in this collection that are contained in the
retainAll(Collection c) specified collection.
size() This method is used to return the number of elements in the collection.
spliterator() This method is used to create a Spliterator over the elements in this collection.
stream() This method is used to return a sequential Stream with this collection as its source.
toArray() This method is used to return an array containing all of the elements in this collection.
Iterator and Iterable Interfaces
Iterator Interface
Method Description
public boolean hasNext() It returns true if the iterator has more elements otherwise it returns
false.
public Object next() It returns the element and moves the cursor pointer to the next
element.
public void remove() It removes the last elements returned by the iterator. It is less used.
Iterable interface
Iterator<T> iterator() It provides iterator over T type of elements
List interface
• Ordered collection of objects
• Can have duplicate objects import java.util.*;
class Test{
• Classes implementing List interface public static void main(String args[]){
• ArrayList: ArrayList<String> list=new ArrayList<String>();//Creating arraylist
• dynamic, resizable, ordered,
• object based, null values, duplicate list.add("Ravi");//Adding object in arraylist
• Non synchronized list.add("Vijay");
• LinkedList: list.add("Ravi");
• dynamic size, list.add("Ajay");
• effective insertion and //Traversing list through Iterator
• deletion, non synchronized
Iterator itr=list.iterator();
• Vector:
• dynamic array while(itr.hasNext()){
• but synchronized System.out.println(itr.next());
• Stack: } }}
List <T> al = new ArrayList<> ();
List <T> ll = new LinkedList<> ();
List <T> v = new Vector<> ();
Where T is the type of the object
Question
• Write a program in java to implement LinkedList, Vector and Stack.
Perform Insertion and deletion of elements in the implemented
collections. Display the elements of implemented collection.
• Write a program in java to demonstrate functioning of priority queue
(PriorityQueue) and double ended queue (Deque).
Set Interface
• Unordered collection of objects
• Does not allow duplicate objects
• Implemented by HashSet, TreeSet, LinkedHashSet
import java.util.*; import java.util.*;
public class Test{ public class Test{
public static void main(String args[]){ public static void main(String args[]){
//Creating HashSet and adding elements //Creating and adding elements
HashSet<String> set=new HashSet<String>(); TreeSet<String> set=new TreeSet<String>();
set.add("Ravi"); set.add("Ravi");
set.add("Vijay"); set.add("Vijay");
set.add("Ravi"); set.add("Ravi");
set.add("Ajay"); set.add("Ajay");
//Traversing elements //traversing elements
Iterator<String> itr=set.iterator(); Iterator<String> itr=set.iterator();
while(itr.hasNext()){ while(itr.hasNext()){
System.out.println(itr.next()); System.out.println(itr.next());
} }} } }}
Thank You