Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Collection(I) Java Framework

1. List(I)

2. set(I)

3. queue(I)

1. List(I)

1. duplicate are allowed in list

2. allows any no of null values

3. Storage Type: index

4. order of insertion-maintain

1. Arraylist(IC)

2. vector(IC)------legecy
3. LinkedList(IC)

2. Set(I)

1. doesn't allow duplicate

2. allow only 1 null value(except treeset)

3. order of insertion-random insertion

1. Hashset(IC)

2. LinkedHashset(IC)

3. Treeset(IC)

3. queue(I)

1. priorityQueue(IC)

Cursor

1.Iterator-- all the types of collection object --universal curser (7)

2. listIterator -- only for list interface type impl classes --not an universal curser(3)

3. enumeration -- legecy --not universal curser(vector-1)

1. Arraylist(IC)
1. duplicate are allowded in Arraylist

2. allows any no of null values

3. storage type: index

4. order of insertion-maintain

5. Default capacity for arraylist is 10

6. data structure: Resizable

7. Incremental capacity= (current capacity*3/2)+1=10*1.5+1=1616*1.5+1=25

8. best choice: retrival operation (random access interface is implemented in arraylist


& vector)

9. worst choice: manipulation operation i.e. insertion in between arraylist or delete ()

10. storage type: index

ArryList Example

package CollectionUse;

import java.util.ArrayList;
import java.util.Iterator;

//import com.sun.tools.javac.util.Iterators;

public class ArrayListUse {

public static void main(String[] args) {


ArrayList al= new ArrayList();
al.add("Velocity");//0
al.add("May-21");//1
al.add(90);//2
al.add('A');//3
al.add(99.99f);//4
al.add("Velocity");//5
al.add(null);//6
al.add(null);//7

System.out.println(al);
System.out.println(al.size());
System.out.println(al.indexOf("Velocity"));

System.out.println(al.lastIndexOf("Velocity"));
System.out.println(al);
al.set(3, 'B');//update
System.out.println(al);
System.out.println(al.isEmpty());
System.out.println(al.contains(93));
System.out.println(al.get(5));
System.out.println(al);
al.add(1, "MorningBatch");// right
shift--> when you are inserting element in
between arraylist
System.out.println(al);
al.remove(1);//left shit--> when you try
to remove/delete from in between of arraylist
System.out.println(al);
// Iterator it= al.iterator();
//
// while(it.hasNext())
// {
// System.out.println(it.next());
// }
// for(int i =0; i<=al.size()-1;i++)
//
// {
// System.out.println(al.get(i));
// }
//
for(Object o:al)
{
System.out.println(o);
}

2. Vector

1. duplicate are allowed in vector

2. allows any no of null values


3. storage type: index

4. order of insertion-maintain

5. Default capacity for Vector is 10

*6. data structure: doubly

*7. Incremental capacity= current capacity*2

8. best choice: retrival operation (random access interface is implemented in arraylist


& vector)

9. worst choice: manipulation operation i.e. insertion in between Vector or delete ()

*10. Vector is legecy class.

Vector Example:

package CollectionUse;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Vector;

public class VectorStudy {

public static void main(String[] args) {

Vector v= new Vector();


v.add(100);//0
v.add("Velocity");//1
v.add(88.2f);//2
v.add(100);//3
v.add(null);//4
v.add(null);//5
System.out.println(v);
System.out.println(v.capacity());
System.out.println(v.size());
System.out.println(v.isEmpty());
System.out.println(v.contains(0));
System.out.println(v.indexOf("Velocity"));
System.out.println(v.get(4));
System.out.println(v);
v.add(1, "Pune");// right shift
System.out.println(v);
v.remove(3);//left shift
System.out.println(v);
// for(Object o:v) // for each loop
// {
// System.out.println(o);
// }
// Iterator i= v.iterator();// universal
ietrator
//
// while (i.hasNext())
// {
// System.out.println(i.next());
//
// }
// ListIterator li= v.listIterator();//
listIterator--> Applicable for all classes
implements list interface
// while (li.hasNext()) {
//
// System.out.println(li.next());
// }

Enumeration en=v.elements();
while(en.hasMoreElements())
{
System.out.println(en.nextElement());
}

3. LinkedList

1. duplicate are allowed in LinkeList

2. allows any no of null values

3. order of insertion-maintain

4. storage type: index

* 5. no Default capacity in linkedlist


*6. data structure: linear

*7. best choice: manipulation operation i.e. insertion in between linkedlist or delete()

*8. worst choice: retrival operation (random access interface is not implemented)

Linked List Example:

package Collection;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class example3_Linkedlist {

public static void main(String[] args) {


LinkedList ll=new LinkedList();
ll.add("abc");
ll.add(100);
ll.add('A');
ll.add(65.5f);
ll.add(100);
ll.add(null);
ll.add(null);
System.out.println(ll);
System.out.println(ll.size()); //7
System.out.println(ll.isEmpty());
//false
System.out.println(ll.contains(100));
//true
System.out.println(ll.indexOf(100));
//1
System.out.println(ll.lastIndexOf(100));
//4
System.out.println(ll.get(2)); //A
System.out.println(ll);
ll.add(4,200); //insert info in
between linkedlist
System.out.println(ll);
ll.remove(4); //remove info in
between linkedlist
System.out.println(ll);
ll.set(0, "xyz");
System.out.println(ll);

System.out.println("-----print info in
linkdlist using iterator cursor------");
Iterator itr = ll.iterator();
while(itr.hasNext()) {
System.out.println(itr.next());
}
System.out.println("-----print info in
linkdlist using Listiterator cursor------");
ListIterator litr = ll.listIterator();
while(litr.hasNext()) {
System.out.println(litr.next());
}
System.out.println("-----print info in
linkdlist using for loop------");
for(int i=0; i<=ll.size()-1; i++) {
System.out.println(ll.get(i));
}

System.out.println("-----print info in
linkdlist using foreach loop------");
for(Object s1:ll)
{
System.out.println(s1);
}
}
}

Arraylist Vector
not legacy class legacy class
DS: resizable DS: doubly
I.C=(C.C.*3/2)+1 IC=CC*2
non-synchronized synchronized
not-thread safe thread-safe
performance: high performance: low

List Set
duplicate: allowed duplicate: not allowed
Any no of null values only one null value-except treeset
order of insertion-maintain random insertion --> random/maintain/asscending
storage type- index storage type- hashtable
2. Set(I)

1. doesn't allow duplicate

2. allow only 1 null value(except treeset)

3. order of insertion-random/asscending/maintain insertion

4. storage type- hashtable

1. Hashset(IC)

2. LinkedHashset(IC)

3. Treeset(IC)

1.Hashset:

1. Doesn't allow duplicate values

2. Allow only 1 null value.

*3. order of insertion-random insertion

4. storage type: hashtable

5. no default capacity

6. DS: Hashtable

best choice: To remove duplicate elements when order of insertion is not mandatory.

HashSet Exaple:

package CollectionUse;

import java.util.HashSet;
import java.util.Iterator;

//import com.sun.tools.jdi.EventSetImpl.Itr;

public class Hash_Set_Study {

public static void main(String[] args) {


HashSet h= new HashSet();
h.add(100);
h.add("Velocity");
h.add('A');
h.add(100);// added duplicate
h.add(66.7f);
h.add(null);
h.add(null);

System.out.println(h);
System.out.println(h.size());
System.out.println(h.contains('A'));
System.out.println(h.isEmpty());
System.out.println(h);
System.out.println(h.remove('A'));
System.out.println(h);

Iterator it= h.iterator();


while(it.hasNext())
{
System.out.println(it.next());
}

for(Object o:h)
{
System.out.println(o);
}

h.clear();
System.out.println(h);
System.out.println(h.isEmpty());

2. LinkedHasSet:

1. Doesn't allow duplicate values

2. Allow only 1 null value.

*3. order of insertion-maintained

4. no default capacity

*5. DS: Hybrid (liner+ hashtable)

6. storage type: hashtable

best choice: To remove duplicate elements when order of insertion is mandatory

Linked HashSet Example:


package CollectionUse;

import java.util.Iterator;
import java.util.LinkedHashSet;

public class Linked_HashSet_study {

public static void main(String[] args) {


LinkedHashSet lh= new LinkedHashSet();
lh.add(100);
lh.add("velocity");
lh.add('A');
lh.add(null);
lh.add(100);
lh.add(null);
lh.add('B');
System.out.println(lh);
System.out.println(lh.isEmpty());
System.out.println(lh.size());
System.out.println(lh.contains('B'));
System.out.println(lh);
lh.remove(100);
System.out.println(lh);
Iterator it= lh.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}

for(Object o:lh)
{
System.out.println(o);
}
lh.clear();
System.out.println(lh);
System.out.println(lh.size());
System.out.println(lh.isEmpty());
}

3. TreeSet:

1. doesn't allow duplicate

*2. null values: not allowed// NullPointerException


*3. order of insertion- Asscending order.

4. no default capacity.

5. DS: Balanced tree

6. storage type: hashtable

note: we can store only homogeneous data

ClassCastException
best choice: To remove duplicate elements when order of insertion is Asscending order.

Treeset Example

package CollectionUse;

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetExample {

public static void main(String[] args) {


TreeSet ts= new TreeSet();
ts.add("xyz");
ts.add("velocity");
ts.add("abc");
ts.add("pune");
ts.add("xyz");
ts.add("XXX");
ts.add("aaa");
ts.add("AAA");
//ts.add(null);--> will throw nullpointer
exception
System.out.println(ts);
System.out.println(ts.isEmpty());
System.out.println(ts.contains("abc"));
System.out.println(ts.pollFirst());
System.out.println(ts);
System.out.println(ts.pollLast());
System.out.println(ts);

Iterator it= ts.iterator();


while(it.hasNext())
{
System.out.println(it.next());
}
for(Object o:ts)
{
System.out.println(o);
}
}

Priority Queue

package CollectionUse;

import java.util.PriorityQueue;

public class PriorityQStudy {

public static void main(String[] args) {


PriorityQueue pq= new PriorityQueue();
pq.add("abc");
pq.add("pqr");
pq.add("lmn");
pq.add("xyz");
pq.add("aaa");
//pq.add(100);
System.out.println(pq);
System.out.println(pq.element());
System.out.println(pq.poll());
System.out.println(pq);
System.out.println(pq.peek());
System.out.println(pq);
pq.poll();
System.out.println("remove method
"+pq.remove());
pq.poll();
pq.poll();
System.out.println(pq);
pq.remove();
//pq.poll();
//pq.remove();
pq.poll();
System.out.println(pq);

collection : interface

collections: class
read

remove

replace

insert new object

Cursors in Collections

1. Iterator

1. All the collection object --> Universal curser


2. Using Enumeration and iterater we can traverse collection object only in forword
direction not a backword --> Single directional cursor
3. By using iterater we can perform only read and remove operation we can not perform
replace and addition of new object.

2. listIterator

1. Only applicable for list interface type implementation classes --> not universal curser
2. Using list iterater we can traverse a List in forward direction and backword direction-->
bidirectional cursor
3. By using listIterator we can perform read, remove, replace and addition of new object
operations.

3. Enumeration

1. Only applicable for legacy classes and hence it is not a universal cursor.
2. Using Enumeration and iterater we can traverse collection object only in forword
direction not a backword --> Single directional cursor
3. By using enumeration we can get only read access.

You might also like