Collection Framework provides an architecture to store and manipulate the group of objects.
All the operations that you perform on a data such as searching, sorting, insertion, deletion etc. can be performed by Java Collection Framework.
Collection simply means a single unit of objects. Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
All the operations that you perform on a data such as searching, sorting, insertion, deletion etc. can be performed by Java Collection Framework.
Collection simply means a single unit of objects. Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
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: |
ArrayList Class:
Example:
import java.util.*;
class Simple{
public static void main(String args[]){
// this form of creating ArayList will accept any object to add
// this is because, add method accepts a parameter of type Object.
ArrayList al=new ArrayList();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add(1.2f); // adding floating point number to ArrayList
al.add(2); // adding integer to ArrayList
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
Two ways to iterate the elements of collection:
a. Using Iterator interface.
b. Using for-each loop.
for(Object obj:al) {
System.out.println(obj);
}
Creating a parameterized ArrayList:
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExample {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add("raghu");
al.add("ramya");
al.add("raghu");
al.add(2);
al.add(1.2f);
Iterator it = al.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
// this ArrayList only accepts Strings.
ArrayList<String> als = new ArrayList<String>();
als.add("raghunath");
als.add("srinath");
// als.add(1.2f); // gives compilation error
for(String name: als)
System.out.println(name);
}
}
LinkedList Class:
Example:
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList al = new LinkedList();
al.add("raghu");
al.add("ramya");
al.add("raghu");
al.add(2);
al.add(1.2f);
Iterator it = al.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
LinkedList<String> als = new LinkedList<String>();
als.add("raghunath");
als.add("srinath");
for(String name: als)
System.out.println(name);
}
}
ListIterator Interface:
ListIterator Interface is used to traverse the element in backward and forward direction.
Methods:
Example:
LinkedList<String> als = new LinkedList<String>();
als.add("raghunath");
als.add("srinath");
for(String name: als)
System.out.println(name);
Iterator it = als.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
System.out.println("Foward Iteration:");
ListIterator fli = als.listIterator();
while(fli.hasNext())
System.out.println(fli.next());
System.out.println("Backward Iteration:");
ListIterator bli = als.listIterator(2);
while(bli.hasPrevious())
System.out.println(bli.previous());
HashSet Class:
The main difference between list and set is, set contain unique elements only. No duplicates are allowed in Set.
import java.util.HashSet;
import java.util.Iterator;
public class HashSetExample {
public static void main(String[] args) {
HashSet hs = new HashSet();
hs.add("raghu");
hs.add("ramya");
hs.add("ajay");
hs.add("vinay");
hs.add("raghu");
hs.add("srinath");
hs.add("vinay");
Iterator it = hs.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}
o/p: You can see here, the duplicates are not printed. Also, items are in random order.
vinay
raghu
srinath
ajay
ramya
LinkedHashSet Class:
Example:
LinkedHashSet al=new LinkedHashSet();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
TreeSet Class:
TreeSet ts = new TreeSet();
ts.add("Ravi");
ts.add("Vijay");
ts.add("Ravi");
ts.add("Ajay");
Iterator titr=ts.iterator();
while(titr.hasNext()){
System.out.println(titr.next());
}
|
No comments:
Post a Comment