collection framework notes
DESCRIPTION
Explicit Details of Collection Framework in Java inclduing its Methods and Interfaces.TRANSCRIPT
Collections in javais a framework that provides an architecture to store and manipulate the group of objects.
What is framework in java
provides readymade architecture.
represents set of classes and interface.
Hierarchy of Collection Framework
Let us see the hierarchy of collection framework.Thejava.utilpackage contains all the classes and interfaces for Collection framework.
Methods of Collection interface
There are many methods declared in the Collection interface. They are as follows:
No.
Method
Description
1
public boolean add(Object element)
is used to insert an element in this collection.
2
public boolean addAll(collection c)
is used to insert the specified collection elements in the
invoking collection.
3
public boolean remove(Object element)
is used to delete an element from this collection.
4
public boolean removeAll(Collection c)
is used to delete all the elements of specified collection
from the invoking collection.
5
public boolean retainAll(Collection c)
is used to delete all the elements of invoking collection
except the specified collection.
6
public int size()
return the total number of elements in the collection.
7
public void clear()
removes the total no of element from the collection.
8
public boolean contains(object element)
is used to search an element.
9
public boolean containsAll(Collection c)
is used to search the specified collection in this collection.
10
public Iterator iterator()
returns an iterator.
11
public Object[] toArray()
converts collection into array.
12
public boolean isEmpty()
checks if collection is empty.
13
public boolean equals(Object element)
matches two collection.
14
public int hashCode()
returns the hashcode number for collection.
Iterator interface
Iterator interface provides the facility of iterating the elements in forward direction only.
Methods of Iterator interface
There are only three methods in the Iterator interface. They are:
1. public boolean hasNext()it returns true if iterator has more elements.
2. public object next()it returns the element and moves the cursor pointer to the next element.
3. public void remove()it removes the last elements returned by the iterator. It is rarely used.
Java ArrayList class
Java ArrayList class uses a dynamic array for storing the elements.It extends AbstractList class and implements List interface.
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 array works at the index basis.
In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
Java Non-generic Vs Generic Collection
Java collection framework was non-generic before JDK 1.5. Since 1.5, it is generic.
Java new generic collection allows you to have only one type of object in collection. Now it is type safe so typecasting is not required at run time.
Let's see the old non-generic example of creating java collection.
1. ArrayListal=newArrayList();//creatingoldnon-genericarraylist
Let's see the new generic example of creating java collection.
1. ArrayListal=newArrayList();//creatingnewgenericarraylist
In generic collection, we specify the type in angular braces. Now ArrayList is forced to have only specified type of objects in it. If you try to add another type of object, it givescompile time error.
For more information of java generics, click hereJava Generics Tutorial.
Example of Java ArrayList class
1. importjava.util.*;
2. classTestCollection1{
3. publicstaticvoidmain(Stringargs[]){
4.
5. ArrayListal=newArrayList();//creatingarraylist
6. al.add("Ravi");//addingobjectinarraylist
7. al.add("Vijay");
8. al.add("Ravi");
9. al.add("Ajay");
10.
11. Iteratoritr=al.iterator();//gettingIteratorfromarraylisttotraverseelements
12. while(itr.hasNext()){
13. System.out.println(itr.next());
14. }
15. }
16. }
Test it Now
Ravi
Vijay
Ravi
Ajay
Two ways to iterate the elements of collection in java
1. By Iterator interface.
2. By for-each loop.
In the above example, we have seen traversing ArrayList by Iterator. Let's see the example to traverse ArrayList elements using for-each loop.
Iterating the elements of Collection by for-each loop
1. importjava.util.*;
2. classTestCollection2{
3. publicstaticvoidmain(Stringargs[]){
4. ArrayListal=newArrayList();
5. al.add("Ravi");
6. al.add("Vijay");
7. al.add("Ravi");
8. al.add("Ajay");
9. for(Stringobj:al)
10. System.out.println(obj);
11. }
12. }
Test it Now
Ravi
Vijay
Ravi
Ajay
User-defined class objects in Java ArrayList
1. classStudent{
2. introllno;
3. Stringname;
4. intage;
5. Student(introllno,Stringname,intage){
6. this.rollno=rollno;
7. this.name=name;
8. this.age=age;
9. }
10. }
1. importjava.util.*;
2. publicclassTestCollection3{
3. publicstaticvoidmain(Stringargs[]){
4. //Creatinguser-definedclassobjects
5. Students1=newStudent(101,"Sonoo",23);
6. Students2=newStudent(102,"Ravi",21);
7. Students2=newStudent(103,"Hanumat",25);
8.
9. ArrayListal=newArrayList();//creatingarraylist
10. al.add(s1);//addingStudentclassobject
11. al.add(s2);
12. al.add(s3);
13.
14. Iteratoritr=al.iterator();
15. //traversingelementsofArrayListobject
16. while(itr.hasNext()){
17. Studentst=(Student)itr.next();
18. System.out.println(st.rollno+""+st.name+""+st.age);
19. }
20. }
21. }
Test it Now
101 Sonoo 23
102 Ravi 21
103 Hanumat 25
Example of addAll(Collection c) method
1. importjava.util.*;
2. classTestCollection4{
3. publicstaticvoidmain(Stringargs[]){
4.
5. ArrayListal=newArrayList();
6. al.add("Ravi");
7. al.add("Vijay");
8. al.add("Ajay");
9.
10. ArrayListal2=newArrayList();
11. al2.add("Sonoo");
12. al2.add("Hanumat");
13.
14. al.addAll(al2);
15.
16. Iteratoritr=al.iterator();
17. while(itr.hasNext()){
18. System.out.println(itr.next());
19. }
20. }
21. }
Test it Now
Ravi
Vijay
Ajay
Sonoo
Hanumat
Example of removeAll() method
1. importjava.util.*;
2. classTestCollection5{
3. publicstaticvoidmain(Stringargs[]){
4.
5. ArrayListal=newArrayList();
6. al.add("Ravi");
7. al.add("Vijay");
8. al.add("Ajay");
9.
10. ArrayListal2=newArrayList();
11. al2.add("Ravi");
12. al2.add("Hanumat");
13.
14. al.removeAll(al2);
15.
16. System.out.println("iteratingtheelementsafterremovingtheelementsofal2...");
17. Iteratoritr=al.iterator();
18. while(itr.hasNext()){
19. System.out.println(itr.next());
20. }
21.
22. }
23. }
Test it Now
iterating the elements after removing the elements of al2...
Vijay
Ajay
Example of retainAll() method
1. importjava.util.*;
2. classTestCollection6{
3. publicstaticvoidmain(Stringargs[]){
4. ArrayListal=newArrayList();
5. al.add("Ravi");
6. al.add("Vijay");
7. al.add("Ajay");
8. ArrayListal2=newArrayList();
9. al2.add("Ravi");
10. al2.add("Hanumat");
11.
12. al.retainAll(al2);
13.
14. System.out.println("iteratingtheelementsafterretainingtheelementsofal2...");
15. Iteratoritr=al.iterator();
16. while(itr.hasNext()){
17. System.out.println(itr.next());
18. }
19. }
20. }
Test it Now
iterating the elements after retaining the elements of al2...
Ravi
Java LinkedList class
Java LinkedList class uses doubly linked list to store the elements. It extends 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 be occurred.
Java LinkedList class can be used as list, stack or queue.
Java LinkedList Example
1. importjava.util.*;
2. publicclassTestCollection7{
3. publicstaticvoidmain(Stringargs[]){
4.
5. LinkedListal=newLinkedList();
6. al.add("Ravi");
7. al.add("Vijay");
8. al.add("Ravi");
9. al.add("Ajay");
10.
11. Iteratoritr=al.iterator();
12. while(itr.hasNext()){
13. System.out.println(itr.next());
14. }
15. }
16. }
Test it Now
Output:Ravi
Vijay
Ravi
Ajay
Difference between ArrayList and LinkedList
ArrayList and LinkedList both implements List interface and maintains insertion order. Both are non synchronized classes.
But there are many differences between ArrayList and LinkedList classes that are given below.
ArrayList
LinkedList
1) ArrayList internally usesdynamic arrayto store the elements.
LinkedList internally usesdoubly linked list
to store the elements.
2) Manipulation with ArrayList isslowbecause it internally uses array. If any element is removed from the array, all the bits are shifted in memory.
Manipulation with LinkedList isfasterthan
ArrayList because it uses doubly linked list
so no bit shifting is required in memory.
3) ArrayList class canact as a listonly because it implements List only.
LinkedList class canact as a list and queueboth because it implements List and Deque interfaces.
4) ArrayList isbetter for storing and accessingdata.
LinkedList isbetter for manipulatingdata.
Example of ArrayList and LinkedList in Java
Let's see a simple example where we are using ArrayList and LinkedList both.
1. importjava.util.*;
2. classTestArrayLinked{
3. publicstaticvoidmain(Stringargs[]){
4.
5. Listal=newArrayList();//creatingarraylist
6. al.add("Ravi");//addingobjectinarraylist
7. al.add("Vijay");
8. al.add("Ravi");
9. al.add("Ajay");
10.
11. Listal2=newLinkedList();//creatinglinkedlist
12. al2.add("James");//addingobjectinlinkedlist
13. al2.add("Serena");
14. al2.add("Swati");
15. al2.add("Junaid");
16.
17. System.out.println("arraylist:"+al);
18. System.out.println("linkedlist:"+al2);
19. }
20. }
Test it Now
Output:
arraylist: [Ravi,Vijay,Ravi,Ajay]
linkedlist: [James,Serena,Swati,Junaid]
Java HashSet class
uses hashtable to store the elements.It extends AbstractSet class and implements Set interface.
contains unique elements only.
Difference between List and Set:
List can contain duplicate elements whereas Set contains unique elements only.
Hierarchy of HashSet class:
Example of HashSet class:
1. importjava.util.*;
2. classTestCollection9{
3. publicstaticvoidmain(Stringargs[]){
4.
5. HashSetal=newHashSet();
6. al.add("Ravi");
7. al.add("Vijay");
8. al.add("Ravi");
9. al.add("Ajay");
10.
11. Iteratoritr=al.iterator();
12. while(itr.hasNext()){
13. System.out.println(itr.next());
14. }
15. }
16. }
Test it Now
Output:Ajay Vijay RaviJava LinkedHashSet class:
contains unique elements only like HashSet. It extends HashSet class and implements Set interface.
maintains insertion order.
Hierarchy of LinkedHashSet class:
Example of LinkedHashSet class:
1. importjava.util.*;
2. classTestCollection10{
3. publicstaticvoidmain(Stringargs[]){
4.
5. LinkedHashSetal=newLinkedHashSet();
6. al.add("Ravi");
7. al.add("Vijay");
8. al.add("Ravi");
9. al.add("Ajay");
10.
11. Iteratoritr=al.iterator();
12. while(itr.hasNext()){
13. System.out.println(itr.next());
14. }
15. }
16. }
Test it Now
Output:>Ravi Vijay AjayJava TreeSet class
contains unique elements only like HashSet. The TreeSet class implements NavigableSet interface that extends the SortedSet interface.
maintains ascending order.
Hierarchy of TreeSet class:
Example of TreeSet class:
1. importjava.util.*;
2. classTestCollection11{
3. publicstaticvoidmain(Stringargs[]){
4.
5. TreeSetal=newTreeSet();
6. al.add("Ravi");
7. al.add("Vijay");
8. al.add("Ravi");
9. al.add("Ajay");
10.
11. Iteratoritr=al.iterator();
12. while(itr.hasNext()){
13. System.out.println(itr.next());
14. }
15. }
16. }
Test it Now
Output:Ajay Ravi Vijay
Java Queue Interface
The Queue interface basically orders the element in FIFO(First In First Out)manner.
Methods of Queue Interface :
1. public boolean add(object);
2. public boolean offer(object);
3. public remove();
4. public poll();
5. public element();
6. public peek();
PriorityQueue class:
The PriorityQueue class provides the facility of using queue. But it does not orders the elements in FIFO manner.
Example of PriorityQueue:
1. importjava.util.*;
2. classTestCollection12{
3. publicstaticvoidmain(Stringargs[]){
4.
5. PriorityQueuequeue=newPriorityQueue();
6. queue.add("Amit");
7. queue.add("Vijay");
8. queue.add("Karan");
9. queue.add("Jai");
10. queue.add("Rahul");
11.
12. System.out.println("head:"+queue.element());
13. System.out.println("head:"+queue.peek());
14.
15. System.out.println("iteratingthequeueelements:");
16. Iteratoritr=queue.iterator();
17. while(itr.hasNext()){
18. System.out.println(itr.next());
19. }
20.
21. queue.remove();
22. queue.poll();
23.
24. System.out.println("afterremovingtwoelements:");
25. Iteratoritr2=queue.iterator();
26. while(itr2.hasNext()){
27. System.out.println(itr2.next());
28. }
29.
30. }
31. }
Test it Now
Output:head:Amit
head:Amit
iterating the queue elements:
Amit
Jai
Karan
Vijay
Rahul
after removing two elements:
Karan
Rahul
Vijay
Java Map Interface
A map contains values based on the key i.e. key and value pair.Each pair is known as an entry.Map contains only unique elements.
Commonly used methods of Map interface:
1. public Object put(object key,Object value):is used to insert an entry in this map.
2. public void putAll(Map map):is used to insert the specified map in this map.
3. public Object remove(object key):is used to delete an entry for the specified key.
4. public Object get(Object key):is used to return the value for the specified key.
5. public boolean containsKey(Object key):is used to search the specified key from this map.
6. public boolean containsValue(Object value):is used to search the specified value from this map.
7. public Set keySet():returns the Set view containing all the keys.
8. public Set entrySet():returns the Set view containing all the keys and values.
Java HashMap class
A HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
It contains only unique elements.
It may have one null key and multiple null values.
It maintains no order.
Hierarchy of HashMap class:
Example of HashMap class:
1. importjava.util.*;
2. classTestCollection13{
3. publicstaticvoidmain(Stringargs[]){
4.
5. HashMaphm=newHashMap();
6.
7. hm.put(100,"Amit");
8. hm.put(101,"Vijay");
9. hm.put(102,"Rahul");
10.
11. for(Map.Entrym:hm.entrySet()){
12. System.out.println(m.getKey()+""+m.getValue());
13. }
14. }
15. }
Test it Now
Output:102 Rahul 100 Amit 101 VijayJava LinkedHashMap class
A LinkedHashMap contains values based on the key. It implements the Map interface and extends HashMap class.
It contains only unique elements.
It may have one null key and multiple null values.
It is same as HashMap instead maintains insertion order.
Hierarchy of LinkedHashMap class:
Example of LinkedHashMap class:
1. importjava.util.*;
2. classTestCollection14{
3. publicstaticvoidmain(Stringargs[]){
4.
5. LinkedHashMaphm=newLinkedHashMap();
6.
7. hm.put(100,"Amit");
8. hm.put(101,"Vijay");
9. hm.put(102,"Rahul");
10.
11. for(Map.Entrym:hm.entrySet()){
12. System.out.println(m.getKey()+""+m.getValue());
13. }
14. }
15. }
Test it Now
Output:100 Amit 101 Vijay 103 RahulJava TreeMap class
A TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
It contains only unique elements.
It cannot have null key but can have multiple null values.
It is same as HashMap instead maintains ascending order.
Hierarchy of TreeMap class:
Example of TreeMap class:
1. importjava.util.*;
2. classTestCollection15{
3. publicstaticvoidmain(Stringargs[]){
4.
5. TreeMaphm=newTreeMap();
6.
7. hm.put(100,"Amit");
8. hm.put(102,"Ravi");
9. hm.put(101,"Vijay");
10. hm.put(103,"Rahul");
11.
12. for(Map.Entrym:hm.entrySet()){
13. System.out.println(m.getKey()+""+m.getValue());
14. }
15. }
16. }