Java Collections
Java Collections
Framework
Topics
• What is a Java collection framework?
• Why use Java collection?
• Java collection framework Hierarchy
• Interface
• List
• Queue
• Sets
What is a Java Collection Framework?
void add(int index, Object Inserts the specified element at the specified position.
element)
void clear() Removes all the elements from this list.
Return the index in this list of the last occurrence of the specified element, or
int lastIndexOf(Object o) -1 if the list does not contain this element.
Object[] toArray() Returns an array containing all the elements in the list.
void trimToSize() Trims the capacity of this ArrayList instance to be the list’s current size.
import java.util.ArrayList;
public class Main{
public static void main(String[] args) {
ArrayList<String> list1= new ArrayList<String>();
ArrayList list2= new ArrayList();
list1.add("Arun");
list1.add("john");
list1.add("Joe");
list1.add("Joe");
list2.add("Jac");
list2.add(2.2);
list2.add(56);
System.out.println(list1);
System.out.println(list2);
for (int i=0;i<list1.size();i++)
System.out.println(list1.get(i));
for(String ele : list1)
System.out.println(ele);
Iterator itr= list1.iterator();
while(itr.hasNext())
System.out.println(itr.next);
list2.remove("Jac");
System.out.println(list2);
list2.clear();
System.out.println(list2);
Linked List:
Linked List is a sequence of links which contains items. Each link
contains a connection to another link.
Java Linked List class uses two types of Linked list to store the elements:
• Singly Linked List
• Doubly Linked List
Singly Linked List:
Doubly Linked List:
Methods in the linked list are listed below:
Method Description
boolean add( Object o) It is used to append the specified element to the end of the vector.
boolean contains(Object o) Returns true if this list contains the specified element.
void add (int index, Object element) Inserts the element at the specified element in the vector.
boolean remove(Object o) Removes the first occurrence of the specified element from this list.
int indexOf(Object element) Returns the index of the first occurrence of the specified element in this list, or -1.
int lastIndexOf(Object element) Returns the index of the last occurrence of the specified element in this list, or -1.
import java.util.*;
public class LinkedlistExample{
public static void main(String args[]){
LinkedList<String> al=new LinkedList<String>();// creating linked list
al.add("Rachit"); // adding elements
al.add("Rahul");
al.add("Rajat");
Iterator<String> itr = al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Vectors
Vectors are similar to arrays, where the elements of the vector object can
be accessed via an index into the vector. Vector implements a dynamic
array. Also, the vector is not limited to a specific size, it can shrink or grow
automatically whenever required.
It is similar to ArrayList, but with two differences :
• Vector is synchronized.
• Vector contains many legacy methods that are not part of the
collections framework.
• Syntax:
Vector object = new Vector(size,increment);
Method Description
boolean add(Object o) Appends the specified element to the end of the list.
boolean remove(Object o) Removes the first occurrence of the specified element from this list.
boolean contains(Object
Returns true if this list contains the specified element.
element)
int
indexOfObject (Object eleme Returns the index of the first occurrence of the specified element in the list, or -1.
nt)
Return the index of the last occurrence of the specified element in the list, or -1 if the list does not contain any
int lastIndexOf(Object o)
element.
System.out.println("Size is: "+vec.size());
System.out.println("Default capacity is: "+vec.capacity());
//Display Vector elements
System.out.println("Vector element is: "+vec);
vec.addElement("Rat");
vec.addElement("Cat");
vec.addElement("Deer");
//Again check size and capacity after two insertions
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after addition is: "+vec.capacity());
//Display Vector elements again
System.out.println("Elements are: "+vec);
//Checking if Tiger is present or not in this vector
if(vec.contains("Tiger"))
{
System.out.println("Tiger is present at the index " +vec.indexOf("Tiger"));
}
else
{
System.out.println("Tiger is not present in the list.");
}
//Get the first element
System.out.println("The first animal of the vector is = "+vec.firstElement());
//Get the last element
System.out.println("The last animal of the vector is = "+vec.lastElement());
Queue
Queue in Java follows a FIFO approach i.e. it orders the elements in
First In First Out manner. In a queue, the first element is removed first
and last element is removed in the end.
The elements of the priority queue are ordered according to their
natural ordering, or by a Comparator provided at the queue
construction time. The head of this queue is the least element with
respect to the specified ordering.
Method Description
Inserts the specified element into the queue
boolean add(object)
and returns true if it is a success.
• A map contains values on the basis of key, i.e. key and value pair. Each
key and value pair is known as an entry. A Map contains unique keys.
Class Description
HashMap HashMap is the implementation of Map, but it doesn't maintain
any order.
LinkedHashMap LinkedHashMap is the implementation of Map. It inherits
HashMap class. It maintains insertion order.
TreeMap TreeMap is the implementation of Map and SortedMap. It
maintains ascending order.