Collections
Framework
1.Collections
framework is a semi developed application to provide the reusability and
fecilities to store group of objects as a single entity known as OBJECT.
2.Collection
classes are used to store the group of Heterogeneous and Homogeneous objects.
3.java.util
package consists of all these classes and interfaces.
4.
Collection is a main interface , from this all are derived.
Java
Collection Framework Diagram
List:--it
maintains the data with duplicates
Set:--
-it maintains the data without duplicates
Map:---
-it maintains the data with in
key-value pair format .
List
Interface
1.This
interface is used to get the data in
ordered format or insertion order.
2.it
can allow duplicates
List
Interface implemented in
1.ArrayList 2.Vector 3.Stack ….etc.
ArrayList(non
synchronized)
ArrayList
class is used to store group of objects and we can access the ArrayList values
randomply. It is non-synchronised and can be accessed
elements fastly.
Ex:
import
java.util.*;
class ArrayListExample{
public static void main(String[] args) {
ArrayList a1=new ArrayList();
a1.add("c");
a1.add("2");
a1.add(new
ArrayListExample());//unreferenced object
a1.add("3");
System.out.println(a1.size());
System.out.println("The
values are:"+a1);
System.out.println(a1.remove("3"));
System.out.println(a1);
}
}
Vector
1.It
is used to store group of objects in a given sequence.
2.This
VECTOR is automatically
growable(enlarge) and shrinkable(suppressed).
3.Vector
is Synchrinised and slow in accessing compare to ArrayList.
4.Vector
and ArrayList both are doing same but ArrayList is not Sysnchronised.
5.Vector
takes default capacity as 10 initially.
6.If
size we given Vector(3,2)—3 is initial size and 2 is incremental value,
import
java.util.*;
class
VectorEx{
public static void main(String[] args) {
Vector v=new Vector(3,2);
v.addElement("3");
v.addElement("b");
v.addElement("g");
v.addElement("y");
System.out.println("elements
are:\n");
System.out.println(v);
System.out.println(v.size());
v.addElement("p");
v.addElement("5");
System.out.println(v);
System.out.println(v.size());
System.out.println(v.capacity());
}
}
Stack
Stacks
stores the elements in “ last in first out”, we call it as LIFO.
import
java.util.*;
class
StackEx {
public static void main(String[] args) {
Stack st=new Stack();
st.push("g");
st.push("4");
st.push("ui");
st.push("a");
System.out.println(st);
System.out.println(st.search(“g”));
st.pop();
System.out.println(st);
}
}
HasSet
Not
in insertion order, takes Random Manner.will not allow duplicate values.
Ex:----
import
java.util.*;
class
HashSetDemo {
public static void main(String[] args){
HashSet hs=new HashSet();
hs.add("d");
hs.add("e");
hs.add("f");
hs.add("b");
System.out.println(hs);
}
}
TreeSet
It
sorts elements based on its natural sorting algorithm.
import
java.util.*;
class
TreeSetdDmo {
public static void main(String[] args) {
TreeSet ts=new TreeSet();
ts.add("c");
ts.add("a");
ts.add("b");
System.out.println(ts);
}
}
SortedSet
It
sorts in normal sorting.
Iterator
interface is used to iterate the values present in the OBJECT it is used as
bellow.
Iterator
it = set.iterator();
Iterator
—interface, set ---object ,
iterator()—method.
We
get the values through ---
it.hasNext()---returns
Boolean, if any next value exists returns
true.
it.next():--
it returns next element .
import
java.util.*;
class SortedSetExample{
public
static void main(String[] args) {
SortedSet set= new TreeSet();
set.add("b");
set.add("c");
set.add("f");
set.add("d");
set.add("e");
Iterator it=set.iterator();
while(it.hasNext()){
Object element=it.next();
System.out.println(element.toString());
} } }
LinkedList
Takes sequence order
means it inserts whatever the order we follow while inserting.
import
java.util.*;
class
LinkedListDemo {
public static void main(String[] args) {
LinkedList ll=new LinkedList();
ll.add("f");
ll.add("b");
ll.addFirst("a");
ll.add(1,"a2");
System.out.println("Original
contents of ll:"+ll);
ll.remove("f");
ll.remove(2);
System.out.println("contents
of ll after deletion:"+ll);
ll.removeFirst();
ll.removeLast();
System.out.println("After
deleting first and last contents of ll:"+ll);
Object val=ll.get(2);
ll.set(2,(String)val+"changed");
System.out.println("After
change contents of ll:"+ll);
}
}
Hash
Table
1.
It is a implemented class of Map
interface .It accepts key and value as pair.
2.
It doesn’t strore null keys.
3.
Here we use put() to insert keys and values.
import
java.util.*;
class
HashTableDemo {
public static void main(String[] args) {
Hashtable ht = new
Hashtable();
Enumeration names;
String str;
int sal;
ht.put("abc",5600);
ht.put("bbc",6600); ht.put("cbc",4600);
names=ht.keys();
while(names.hasMoreElements()){
str=(String)names.nextElement();
System.out.println(str+":"+ht.get(str));
}
}
}
Date Class
import java.util.*;
class DateDemo{
public
static void main(String[] args) {
Date
d=new Date();
System.out.println(d);
System.out.println(d.getYear());
System.out.println(d.getMonth());
System.out.println(d.getDay());
System.out.println(d.getHours());
System.out.println(d.getMinutes());
}
}
import java.util.*;
class
CalendarDemo{
public
static void main(String[] args) {
Calendar
c= Calendar.getInstance();
System.out.println(c);
}
}
No comments:
Post a Comment