Collections Framework (1)
Collections Framework (1)
Collections
Framework
Topics
Introduction
Types of collection interfaces
list
set
Queue
Map
What is collection?
A Collection represents a single unit of objects,
i.e., a group.
What is a framework in Java?
•. It provides readymade architecture.
•It represents a set of classes and interfaces.
4
Collection frame work
Collection Object:
A collection object is an object which can store group
of other objects.
A collection object has a class called Collection class or
Container class.
All the collection classes are available in the package
called java.util (util stands for utility).
Group of collection classes is called a Collection
Framework.
A collection object does not store the physical copies
of other objects; it stores references of other objects.
Collection framework
7
1.Collection
2.List
3.Set
4.SortedSet
5.NavigableSet
6.Queue
7.Map
8.SortedMap
9.NavigableMap
Collection framework
Collection Interfaces: 9
The Collection interface is not directly implemented by any
class. However, it is implemented indirectly via its subtypes or
subinterfaces like List, Queue, and Set. For Example, the HashSet
class implements the Set interface which is a subinterface of the
Collection interface.
• It has most common methods
• Such as:add(),remove(),clear(),empty()
• Root interface of colletion frame work
Collection framework
Method of collection interface:
There are many methods declared in the Collection interface. They are 10
as follows:
Methods Description
public boolean add(E e) It is used to insert an element in this
collection.
public boolean addAll(Collection<? extends E> c) It is used to insert the specified collection
elements in the invoking collection.
public boolean remove(Object element) It is used to delete an element from the
collection.
Public boolean removeAll(collection<?>c) It is used to delete all the elements of the
specified collection from the invoking
collection.
default boolean removeIf(Predicate<? super E> filter) It is used to delete all the elements of the
collection that satisfy the specified predicate.
public boolean retainAll(Collection<?> c) It is used to delete all the elements of
invoking collection except the specified
collection.
public int size() It returns the total number of elements in the
collection.
Collection framework
public void clear() It removes the total number of elements from
11
List
Interface
List interface is the child interface of Collection interface. It inhibits a list type data
structure in which we can store the ordered collection of objects. It can have duplicate
values.
List interface is implemented by the classes
• ArrayList
• LinkedList
• Vector
• Stack
Syntax:
1.List <data-type> list1= new ArrayList();
2.List <data-type> list2 = new LinkedList();
3.List <data-type> list3 = new Vector();
4.List <data-type> list4 = new Stack();
Collection framework
12
t:
•Java ArrayList class can contain duplicate elements.
•Java ArrayList class maintains insertion order.
•Java ArrayList class is non synchronized.
•Java ArrayList allows random access because the array works
on an index basis.
•In ArrayList, manipulation is a little bit slower than the
LinkedList in Java because a lot of shifting needs to occur if any
element is removed from the array list.
•We can not create an array list of the primitive types, such as
int, float, char, etc. It is required to use the required wrapper
class in such cases
Collection
framework
import java.util.*;
public class b {
public static void main(String[]args){
List<Integer> k=new ArrayList<Integer>();
13
k.add(31);
k.add(57);
k.add(30);
System.out.println(k);
k.add(1,25);
System.out.println(k);
ArrayList<Integer> m=new
ArrayList<Integer>();
m.add(2);
m.add(7);
k.addAll(m);
Collections.sort(k);
System.out.println(k);
k.remove(1);
System.out.println(k);
System.out.println(k.size());
System.out.println(k.indexOf(57));
m.removeAll(m);
k.clear();
Presentation title System.out.println(m);
System.out.println(k);}}
LinkedList 14
Java LinkedList class uses a doubly linked list to store the
elements. It provides a linked-list data structure. It inherits the
AbstractList class and implements List and Deque interfaces.
•Java LinkedList class can contain duplicate elements.
•Java LinkedList class maintains insertion order.
•Java LinkedList class is non synchronized.
•In Java LinkedList class, manipulation is fast because no
shifting needs to occur.
•Java LinkedList class can be used as a list, stack or queue.
Collection framework
15
C0llection framework
import java.util.*;
public class add{
public static void main(String[]args){
Scanner s=new Scanner(System.in);
16
LinkedList<String> ll=new
LinkedList<String>(); ll.offerLast("sumanth");
LinkedList<String> vi=new System.out.println(ll);
LinkedList<String>(); ll.poll();
ll.pollFirst();
System.out.println("enter string"); System.out.println(ll.clone());
ll.add(s.nextLine()); ll.push("nithin");
ll.add("mohith"); ll.remove();
System.out.println(ll); ll.remove(3);
vi.add("mani"); ll.removeAll(vi);
ll.addAll(0,vi); ll.set(1,"sai");
System.out.println(vi); ll.pop();
System.out.println(ll); System.out.println(ll);
ll.addFirst("karthik"); }
ll.addLast("vamsi"); }
System.out.println(ll.contains("vamsi"));
System.out.println(ll.element());
System.out.println(ll.get(1));
System.out.println(ll);
Collecton framework
ArrayList LinkedList
1.uses a dynamic array to store the 1.uses a doubly linked list to store the
elements. elements.
17
2.act as a list only because it 2.act as a list and queue
implements List only. because it implements List and queue.
3.The memory location for the 3.The memory location for the
elements of an ArrayList is elements of an LinkedList is not
continuous. continuous.
Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-
number of elements in it as there is no size limit. It is a part of Java Collection framework since
Java 1.2. It is found in the java.util package and implements the List interface, so we can use all
the methods of List interface here.
HTML Tutorial
•Vector is synchronized.
•Java Vector contains many legacy methods that are not the part of a
collections framework.
•Heterogenious are allowed
•Dupilicates are allowed
•Null pointer can de inserted
•Follows Insertion order
Collection
framework
19
Stac
k
The stack is a linear data structure that is used to store the collection of objects. It is based
on Last-In-First-Out (LIFO). Java collection
framework provides many interfaces and classes to store the collection of objects. One of
them is the Stack class that provides different operations such as push, pop, search, etc.
Collection framework
20
import java.util.*;
public class staks{
public static void main(String[]args){
Stack <Integer> s = new Stack<>();
s.push(31);
s.push(34);
s.push(30);
System.out.println(s);
System.out.println(s.search(31));
System.out.println(s.pop());
System.out.println(s.peek());
System.out.println(s.empty());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.empty());
}
Presentation title
21
import java.util.*;
public class staks{
public static void main(String[]args){
Stack stk = new Stack();
stk.push("sai");
stk.push(31);
List l=new ArrayList();
l.add("sai");
l.add(31);
List k=new LinkedList();
k.add(31);
k.add("sai");
System.out.println(stk+""+l+""+k);
}
}
Presentation title
22
What is Hashi
ng?
Collection framework
24
Constructors of Hashset
Class
Description
Constructor
HashSet() It is used to construct a default
HashSet.
Method Description
add() It is the element which will be added to this set.
Clear() It is used to return a shallow copy of this HashSet
instance: the elements themselves are not cloned.
Contains() It is used to return true if this set contains the
specified element.
isEmpty() It is used to return true if this set contains no
elements.
System.out.println(s.containsAll(hs));
System.out.println(hs.isEmpty());
System.out.println(hs.contains("sai"));
s.clear();
System.out.println(s);
System.out.println(hs);
hs.remove("pavan");
System.out.println(hs);}
Collection
framework }
27
LinkedHash
Set
Java LinkedHashSet class is a Hashtable and Linked list implementation of the
Set interface. It inherits the HashSet class and implements the Set interface.
Constructor Description
HashSet() It is used to construct a default
HashSet.
HashSet(Collection c) It is used to initialize the hash
set by using the elements of the
collection c.
LinkedHashSet(int capacity) It is used to initialize the
capacity of the linked hash set
to the given integer value
capacity.
LinkedHashSet(int capacity, float It is used to initialize both the
fillRatio) capacity and the fill ratio (also
called load capacity) of the hash
Collection framework set from its argument.
29
Java
TreeSet
Java TreeSet class implements the Set interface that uses a tree for storage. It
inherits AbstractSet class and implements the NavigableSet interface. The
objects of the TreeSet class are stored in ascending order.
•Java TreeSet class contains unique elements only like HashSet.
•Java TreeSet class access and retrieval times are quiet fast.
•Java TreeSet class doesn't allow null element.
•Java TreeSet class is non synchronized.
•Java TreeSet class maintains ascending order.
•The TreeSet can only allow those generic types that are comparable.
•For example The Comparable interface is being implemented by the StringBuffer class.
•Tree sets must be of comparable type if not it shows classcastexception at run time.
•Tree set is of fail-fast nature.means if treeset is modified after the creation of iterator object,you will
get ConcurrentModificationExceotion
Collection
Constructors of Java 30
TreeSet Class
Constructor Description
There are various classes that implement the Queue interface, some of them are
given below.
PriorityQueue
ArrayDeque
Collection framework
32
PriorityQue
ue
o The PriorityQueue class implements the Queue interface. It holds the elements or
objects which are to be processed by their priorities. PriorityQueue doesn't allow
null values to be stored in the queue.
o Not allow null values
o Do not have insertion order
o Insertion performed at the end of queues
o descending order
o Order for deletion of elements from a queue.
Collection
framework
33
ArrayDeque
class
Collection
framework