Professional Documents
Culture Documents
Collection Framework PDF
Collection Framework PDF
Definition of collection
♦ A collection — sometimes called a container — is simply an object that
groups multiple elements into a single unit.
♦ Interfaces
♦ Classes
♦ Algorithm
Collections-related Interface hierarchy
SortedSet
List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.
To instantiate the List interface, we must use :
There are various methods in List interface that can be used to insert, delete, and access the elements from
the list.
Java Collections – List - ArrayList
What is ArrayList?
14. int lastIndexOf(Object item): It returns the index of last occurrence of the specified element.
15. Object poll(): It returns and removes the first item of the list.
16. Object pollFirst(): same as poll() method. Removes the first item of the list.
17. Object pollLast(): It returns and removes the last element of the list.
18. Object remove(): It removes the first element of the list.
19. Object remove(int index): It removes the item from the list which is present at the specified index.
20. Object remove(Object obj): It removes the specified object from the list.
21. Object removeFirst(): It removes the first item from the list.
22. Object removeLast(): It removes the last item of the list.
23. Object removeFirstOccurrence(Object item): It removes the first occurrence of the specified item.
24. Object removeLastOccurrence(Object item): It removes the last occurrence of the given element.
25. Object set(int index, Object item): It updates the item of specified index with the give value.
26. int size(): It returns the number of elements of the list.
Java Collections – List - LinkedListExample
import java.util.*;
public class LinkedListExample {
public static void main(String args[]) {
LinkedList<String> linkedlist = new LinkedList<String>();
Output ????
linkedlist.add("Item1");//adding elements Linked List Content: [Item1, Item5, Item3,
linkedlist.add("Item5"); Item6, Item2]
linkedlist.add("Item3");
linkedlist.add("Item6"); LinkedList Content after addition: [First Item,
linkedlist.add("Item2"); Item1, Item5, Item3, Item6, Item2, Last Item]
System.out.println("Linked List Content: " +linkedlist);
linkedlist.addFirst("First Item");//add first elements LinkedList after deletion of first and last
linkedlist.addLast("Last Item");//add last element element: [Item1, Item5, Item3, Item6, Item2]
System.out.println("LinkedList Content after addition: " +linkedlist);
linkedlist.removeFirst(); Final Content: [Newly added item, Item1,
linkedlist.removeLast(); Item3, Item6, Item2]
System.out.println("LinkedList after deletion of first and last element: "
+linkedlist);
linkedlist.add(0, "Newly added item");
linkedlist.remove(2);
System.out.println("Final Content: " +linkedlist);
ArrayList vs Vector
ArrayList Vector
Resize ArrayList grow by half of its size when Vector doubles the size of itself by
resized by default. default when grows.
Performance ArrayList gives O(n) in worst case. LinkedList add method gives O(1)
performance
Memory Overhead ArrayList maintains indexes and LinkedList maintains element data and
element data two pointers for neighbor nodes hence
the memory consumption is high in
LinkedList comparatively.
Java Collections – List - Stack
The Java Stack interface represents a classical stack data structure, where elements can be pushed to last-in-first-out
(LIFO) stack of objects. In Stack we push an element to the top of the stack, and popped off from the top of the stack
again later.
class Test
{ static void stack_push(Stack<Integer> stack) // Pushing element on the top of the stack
{
for(int i = 0; i < 5; i++)
{
stack.push(i);
}}
static void stack_pop(Stack<Integer> stack) // Popping element from the top of the stack
{ Output ????
System.out.println("Pop :");
for(int i = 0; i < 5; i++)
{ Pop :
Integer y = (Integer) stack.pop();
System.out.println(y);
4
}} 3
static void stack_peek(Stack<Integer> stack) // Displaying element on the top of the stack
{ 2
Integer element = (Integer) stack.peek();
System.out.println("Element on stack top : " + element); }
1
static void stack_search(Stack<Integer> stack, int element) // Searching element in the stack { 0
Integer pos = (Integer) stack.search(element);
Element on stack top : 4
if(pos == -1) Element is found at position 3
System.out.println("Element not found");
else Element not found
System.out.println("Element is found at position " + pos); }
public static void main (String[] args)
{
Stack<Integer> stack = new Stack<Integer>();
stack_push(stack);
stack_pop(stack);
stack_push(stack);
stack_peek(stack);
stack_search(stack, 2);
stack_search(stack, 6); } }
Hierarchy of Map
Java Map Interface
❖ 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.
❖ A Map is useful if you have to search, update or delete elements on the basis of a key.
❖ A Map doesn't allow duplicate keys, but you can have duplicate values. HashMap and
LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or
value.
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.
Java Map -HashMap-Example
import java.util.*;
class MapExample2{
public static void main(String args[]){
Output ????
Map<Integer,String> map=new HashMap<Integer,String>();
//Adding elements to map
map.put(100,"Amit"); Map Elements
map.put(101,"Vijay"); {100=Amit, 101=Vijay, 102=Rahul}
map.put(102,"Rahul"); Map Elements
System.out.println(" Map Elements"); {100=Amit, 101=Vijay}
System.out.print("\t" + map);
map.remove(102); //removing element
System.out.println(" Map Elements");
System.out.print("\t" + map);
}
}
Java Set Interface
System.out.println(hash_Set);