Sunteți pe pagina 1din 15

Collection has following methods

1 public boolean add(Object element) is used to insert an element in this collection.


is used to insert the specified collection elements in the invoking
2 public boolean addAll(Collection c)
collection.
public boolean remove(Object
3 is used to delete an element from this collection.
element)
public boolean is used to delete all the elements of specified collection from the
4
removeAll(Collection c) invoking collection.
public boolean retainAll(Collection is used to delete all the elements of invoking collection except the
5
c) 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.
public boolean contains(Object
8 is used to search an element.
element)
public boolean
9 is used to search the specified collection in this collection.
containsAll(Collection c)
10 public Iterator iterator() returns an iterator.
11 public Object[] toArray() converts collection into array.
12 public boolean isEmpty() checks if collection is empty.
public boolean equals(Object
13 matches two collection.
element)
returns the hashcode number for collection.
14 public int hashCode()

LIST

List has following methods


void add(int index,Object It is used to insert element into the invoking list at the index passed in the
element) index.
boolean addAll(int It is used to insert all elements of c into the invoking list at the index passed
index,Collection c) in the index.
It is used to return the object stored at the specified index within the
object get(int index)
invoking collection.
object set(int index,Object It is used to assign element to the location specified by index within the
element) invoking list.
It is used to remove the element at position index from the invoking list and
object remove(int index)
return the deleted element.
ListIterator listIterator() It is used to return an iterator to the start of the invoking list.
It is used to return an iterator to the invoking list that begins at the specified
ListIterator listIterator(int index)
index.
Int indexOf(Object O) Returns index of Object O in List
Int lastindexOf(Object O) Returns last index of Object O in 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

ArrayList l1=new ArrayList();


LinkedList l2=new LinkedList();
S.O.P(l1 instance of Serializable); t
S.O.P(l2 instance of Cloneable); t
S.O.P(l1 instance of RandomAccess); t
S.O.P(l2 instance of RandomAccess); f

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.

Difference between Arraylist and vector

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

How to get synchronized Arraylist?


Ans:We define arraylist as
ArrayList al=new Arraylist(); //this is non synch
To make it synch
List l=Collections.synchronizedList(al);

So using Collections.synchronizedList(List l); we can turn arraylist to synchronized


Method signature is :-
Public static List synchronizedList(List L);
||y
Public static Set synchronizedSet(Set s); and
Public static Map synchronizedMap(Map m);
Linked List
uNDERLYING DATA STRUCTURE :-Doubly Linked List

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 .

To define an interator on Collection C


Iterator itr=c.iterator();

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.

IF numbers are there Ascending order is default sorting order


If String is there Alphabetic sorting order is there
TREESET

Underlying data structure is balanced tree.


Duplicates ARE NOT ALLOWED .
Insertion Order is not available .So some sorting order is there .
Heterogeneous Objects are not allowed.It will lead to RuntimeException,CCE
Null is Allowed but only once .

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)

In Treeset Null insertion is not allowed as we cannot compare it with anything .

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.

S-ar putea să vă placă și