Documente Academic
Documente Profesional
Documente Cultură
LIST
To transfer Collection from one place to another the serial order must be preserved.
That is why serialization is compulsory .Every Collection Class implements Serlizable Interface as well.
When Collection is sent after that receiver doesnot directly uses Collection.For security aspects it creates a clone of that
and then uses the clone.So Collection implements cLONEABLE INTERFACE AS WELL
In case of Arraylist and Vector Class it implements RANDOM ACCESS INTERFACE so that any element can be accessed in
same unit time.
It is available only in Arraylist and Vector
RANDOM ACCESS INTERFACE is in java.util.* .And it does not have any method .It is marker interface
If we insert/remove at middle of the arraylist then after insertion/removing it should move all elements .So for these
operations arraylist is not preferred.
Arraylist Vector
Most methods are non synchronized Almost all methods are synchronized
Not Thread Safe.Multiple threads can operate Thread Safe.Only one thread can operate
Relatively high performance as multiple threads Low performance as only one thread
ca noperate
1.2v 1.0v Legace Class
VECTOR
Constructors
1)Vector v=new Vector(); //intial capacity -10;if 10 exceeds after that 2*previous capacity
2)Vector v=new Vector(int initialcapacity); after initialcapacity -> 2* initialcapacity
3)Vector v=new Vector(int initialcapacity,int incrementalcapacity);
New size-> initialcapacity + incrementalcapacity;
4)Vector v=new Vector(Collection c);
STACK
Stack s=new Stack();
s.push(Object O);
s.pop();
s.peek();
s.empty();
s.search(object o);
search gives offset from top. If object is not available it will return -1;
ITERATORS
ENUMERATIONS
Guess output of this
import java.util.*;
import java.lang.*;
class Vectenum{
public static void main(String args[]){
Vector v=new Vector(10,3);
for(int i=0;i<=10;i++)
{
v.add(i);
}
System.out.println(v);
System.out.println(v.size());
Enumeration e=v.elements();
while(e.hasMoreElements())
{
if((Integer)e.nextElement()%2==0)
System.out.println(e.nextElement());
}}}
Enumeration Is applicable only for legacy classes.
It can only read objects from collection. We cant remove objects using Enumeration
So Iterator were introduced
ITERATOR
1)Applicable on all collection
2)Able to remove objects .
ITERATOR :- Limitations:-
1)Although we can remove but we cant add/replace objects
2)We can move unidirectional.We cant go back
LIST ITERATOR
1)Bidirectional
2)We can add,remove,replace objects from collection
List Iterator is available only for List Interface
All Cursors are Interfaces .So how Can we make object of these.
Ans:-Actually the Interfaces are internally implemented in some method which creates some class that implements and
return object of that Class Type. Thus we get returntype of Interfaces
HASHSET
SORTED SET
METHODS IN SORTEDSET
100,101,103,104,107,110,115
Object first() 100
Object last() 115
SortedSet headset(104) [100,101,103]
SortedSet tailset(104) [104,107,110,115]
SortedSet subset(103,110) [103,104,107]
Comparator comparator() Returns Comparator Object that describes underlying sorting technique.
If we are using default natural sorting order then we will get null.
Constructors in TreeSet
1)TreeSet t=new TreeSet().
Creates an empty treeset with no elements and Default Natural Sorting Order.
If we want to create TreeSet based on our own sorting order we need different type of Constructor
2)TreeSet t=new TreeSet(Comparator c)
Comparator c gives customized sorting
3)TreeSet t=new TreeSet(Collection C)
4) TreeSet t=new TreeSet(SortedSet s)
With default natural sorting order We cannot use StringBufer to insert elements in TreeSet as StringBuffer doesnot
implements Comparable method so is not comparable by default
import java.util.*;
class TreeDemo
{
public static void main(String args[])
{
TreeSet t=new TreeSet();//dfno
String s=null;
String str="abc";
t.add(new StringBuffer("AB"));
t.add(new StringBuffer("CD"));
t.add(new StringBuffer("ED"));
t.add(new StringBuffer("AA"));
/* t.add('A');
t.add('X');
t.add('s');
t.add('Z');
t.add('E');
*/
System.out.println(t);
}
TreeSet definitely does not implements comparsion based on ASCII values .For e.g.
import java.util.*;
class TreeDemo
{
public static void main(String args[])
{
TreeSet t=new TreeSet();//dfno
String s=null;
String str="abc";
/* t.add(new StringBuffer("AB"));
t.add(new StringBuffer("CD"));
t.add(new StringBuffer("ED"));
t.add(new StringBuffer("AA")); */
t.add("AA");
t.add("aaa");
t.add("zz");
t.add("ZZZ");
t.add("AAZX");
/* */
System.out.println(t);
}
COMPARABLE
The interface is present in java.lang package and it contains only one method CompareTo().
Public int CompareTo(Object j);
Example :
Obj1.CompareTo(obj2);
Returns -ve if obj1 is to be kept before obj2 i.e. obj1<obj2
Returns +ve if obj2 is to be kept before obj1 i.e. obj2<obj1
Returns -ve if both are equal obj1==obj2
The compareTo methods compare 1st digits of both the objects if both are not equals it breaks there only and
returns compared value based on comparsion done on 1st digit only .It will not move to 2nd digit
For e.g.
import java.util.*;
class TreeDemo
{
public static void main(String args[])
{
TreeSet t=new TreeSet();//dfno
String s=null;
String str="abc";
System.out.println("AA".compareTo("aaa"));
System.out.println("Z".compareTo("B"));
System.out.println("AAZX".compareTo("AAZY"));
System.out.println("zzzzzzzzzzzzzzzzzzzzzzzzzz".compareTo("A"));
System.out.println("A".compareTo("zzzzzzzzzzzzzzzzz"));
System.out.println("z".compareTo("AAAAAAAAAA"));
System.out.println("AAAAAAAAAAAAAAA".compareTo("z"));
t.add("AA");
t.add("aaa");
t.add("zz");
t.add("ZZZ");
t.add("AAZX");
t.add("AAZY");
System.out.println(t);
}
}Output :
ADMINIBM@pipande2 MINGW32 ~/Desktop/java prc
$ java TreeDemo
-32
24
-1
57
-57
57
-57
[AA, AAZX, AAZY, ZZZ, aaa, zz]
Comparator Interface
Comparator interface has two methods compare()and equals().
1)public int compare(Obj1,obj2);
Returns -ve if obj1 is to be kept before obj2 i.e. obj1<obj2
Returns +ve if obj2 is to be kept before obj1 i.e. obj2<obj1
Returns -ve if both are equal obj1==obj2
2)public Boolean equals();
In order to implement a interface we need to use all of its methods .So we need to implement both compare
and equals together ,whenerver we have to use any single one of it.
But equals method is already there in root Class -> OBJECT CLASS .So we need not to implement equals()
method.