Professional Documents
Culture Documents
FRAMEWORK
Collection Framework
Collection Framework
A collection is an object that represents a group
of objects. A collection — sometimes
called a container — is simply an object that
groups multiple elements into a single unit.
Collections are used to store, retrieve, manipulate,
and communicate aggregate data.
A collections framework is a unified architecture
for representing and manipulating
collections, allowing them to be manipulated
independently of the details of their
representation.
ArrayList
ArrayList is a part of collection framework and
is present in java.util package. It provides us
dynamic arrays in Java.
Resizable-array implementation of the List
interface
As elements are added to an ArrayList, its
capacity grows automatically.
ArrayList
import java.util.*;
class arrlist
{
public static void main(String[] args)
{
ArrayList <String>ar=new ArrayList<String>();
ar.add("one"); ar.add("two"); ar.add("three");
System.out.println(ar);
ar.add(2,"four");
System.out.println(ar);
System.out.println(ar.size());
ar.remove("two");
ar.remove(2);
System.out.println(ar);
ArrayList <Integer>a=new ArrayList<Integer>();
a.add(1); a.add(2); a.add(18); a.add(15);
System.out.println(a);
a.remove(2);
System.out.println(a);
}
}
LinkedList
The LinkedList class implements the List
interface.
All of the operations perform as could be
expected for a doubly-linked list.
LinkedList and Iterator
import java.util.*;
class linkeg
{
public static void main(String[] args)
{
LinkedList<String> ar=new LinkedList<String>();
ar.add("Banana"); ar.add("Apple");
ar.add("Orange"); ar.add(2," Watermelon");
ar.addFirst("Pear"); ar.addLast("Chikoo");
Iterator<String> it=ar.iterator();
while(it.hasNext())
{
String ele=it.next();
System.out.println(ele);
}
ar.removeFirst();
ar.removeLast();
System.out.println(ar);
}
}
Iterator
The Iterator interface provides methods using
which we can traverse any collection.
This interface is implemented by all collection
classes.
Methods:
hasNext()- returns true if there is a next element
in the collection.
next()- Returns the next object.
remove()-Removes the most recent element
that was returned by next()
ArrayList with iterator
import java.util.*;
class arrlist_it
{
public static void main(String[] args)
{
ArrayList<String> ar=new ArrayList<String>();
ar.add("Banana"); ar.add("Apple");
ar.add("Orange"); ar.add(2,"Watermelon");
Iterator<String> it=ar.iterator();
while(it.hasNext())
{
String ele=it.next();
System.out.println(ele);
}
}
}
ListIterator
It is implemented only by the classes that
implement the List interface (ArrayList,
LinkedList, and Vector).
It has following methods
hasNext()
next()
hasPrevious
previous()
remove()
TreeSet
Java TreeSet class implements the Set
interface that uses a tree for storage.
TreeSet
The important points about Java TreeSet class
are:
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.
TreeSet and Iterator
import java.util.*;
class treeset //sorted in ascending order for elements
{
public static void main(String[] args)
{
TreeSet<String> ar=new TreeSet<String>();
ar.add("Banana"); ar.add("Apple");
ar.add("Orange"); ar.add("Chikoo");
ar.add("Guava"); ar.add("Watermelon");
ar.add("Sweetlime");
Iterator<String> it=ar.iterator();
while(it.hasNext())
{
String ele=it.next();
System.out.println(ele);
}