Collection
Collection
Collections Interfaces
Collection interface methods
void clear( )
int hashCode( )
boolean isEmpty( )
int size( )
List Interface
SortedSet Interface
SortedSet Methods
Object first( )
Returns the first element in the invoking sorted set.
Object last( )
Returns the last element in the invoking sorted set.
SortedSet subSet(Object start, Object end)
Returns a SortedSet that includes those elements
between start and end-1. Elements in the returned collection are
also referenced by the invoking object.
SortedSet headSet(Object end)
Returns a SortedSet containing those elements less than
end that are contained in the invoking sorted set.
SortedSet tailSet(Object start)
Returns a SortedSet that contains those elements greater
than or equal to start that are contained in the sorted set.
Enumeration & Iterator
ListIterator
----------------
next() hasNext()
previous() hasPrevious()
Remove()
LinkedList
The LinkedList class extends AbstractSequentialList and
implements the List interface.
Apart from the methods inherited from its parent classes,
LinkedList defines following methods
LinkedList Methods
void addFirst(Object o)
Inserts the given element at the beginning of this list.
void addLast(Object o)
Appends the given element to the end of this list.
Object getFirst()
Returns the first element in this list.
Object getLast()
Returns the last element in this list
Object removeFirst()
Removes and returns the first element from this list.
Object removeLast()
Removes and returns the last element from this list.
LinkedList Example
import java.util.*;
class LinkedListDemo {
public static void main(String args[]) {
LinkedList ll = new LinkedList();
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
ll.addLast("Z");
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("ll after deleting first and last:"+ll);
}}
ArrayList
The ArrayList class extends AbstractList and implements
the List interface. ArrayList supports dynamic arrays that
can grow as needed.
Standard Java arrays are of a fixed length. After arrays are
created, they cannot grow or shrink, which means that you
must know in advance how many elements an array will
hold.
import java.util.*;
class ArrayListDemo {
public static void main(String args[]) {
ArrayList al = new ArrayList();
System.out.println("Initial size of al:"+al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions:"+al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions:"+al.size());
System.out.println("Contents of al: " + al);
}}
Initial size: 0
Initial capacity:3
Capacity after four additions:5
Current capacity:5
Current capacity:7
First element:1
Last element:7
Vector contains 3.
Elements in vector:
1 2 3 4 5.45 6.08 7
Stack
Stack Example
import java.util.*;
class stackDemo {
public static void main(String args[]) {
Stack s=new Stack();
s.push(new Integer(10));
s.push(new Integer(20));
s.push(new Integer(30));
System.out.println("Elements:"+s);
System.out.println(s.peek());
System.out.println("Elements:"+s);
s.pop();
System.out.println("Elements:"+s);
}}
HashSet
HashSet extends AbstractSet and implements the Set
interface.
HashSet Example
import java.util.*;
class HashSetDemo {
public static void main(String args[]) {
HashSet hs = new HashSet();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
} }
[D, E, F, A, B, C]
HashSet Example
import java.util.*;
class HashSetDemo {
public static void main(String args[]) {
HashSet hs = new HashSet();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
hs.add("A");//duplicate value
System.out.println(hs);
}}
[D, E, F, A, B, C]
LinkedHashSet
import java.util.*;
class HashSetDemo {
public static void main(String args[]) {
LinkedHashSet hs = new LinkedHashSet();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs); } }
[B, A, D, E, C, F]
TreeSet
[A, B, C, D, E, F]
TreeSet Example
import java.util.*;
class HashSetDemo {
public static void main(String args[]) {
TreeSet hs = new TreeSet(Collections.reverseOrder());
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}}
Map Interface
The Map interface maps unique keys to values. A key is an
object that you use to retrieve a value at a later date.
Given a key and a value, you can store the value in a Map
object. After the value is stored, you can retrieve it by
using its key.
Map Interface Methods
void clear( )
Removes all key/value pairs from the invoking map.
boolean containsKey(Object k)
Returns true if the invoking map contains k as a key.
Otherwise, returns false.
boolean containsValue(Object v)
Returns true if the map contains v as a value. Otherwise,
returns false.
Object get(Object k)
Returns the value associated with the key k.
boolean isEmpty( )
Returns true if the invoking map is empty. Otherwise,
returns false.
Object put(Object k, Object v)
Puts an entry in the invoking map, overwriting any
previous value associated with the key. The key and value are
k and v, respectively. Returns null if the key did not already
exist. Otherwise, the previous value linked to the key is
returned.
Object remove(Object k)
Removes the entry whose key equals k.
int size( )
Returns the number of key/value pairs in the map.
SortedMap
The SortedMap interface extends Map.
It ensures that the entries are maintained in ascending key
order
SortedMap Methods
Object firstKey( )
Returns the first key in the invoking map.
SortedMap headMap(Object end)
Returns a sorted map for those map entries with keys that
are less than end.
Object lastKey( )
Returns the last key in the invoking map.
SortedMap subMap(Object start, Object end)
Returns a map containing those entries with keys that are
greater than or equal to start and less than end
SortedMap tailMap(Object start)
Returns a map containing those entries with keys that are
greater than or equal to start.
HashMap Example
import java.util.*;
class hashmapdemo{
public static void main(String args[]){
HashMap h=new HashMap();
h.put(1,new String("one"));
h.put(5,new String("five"));
h.put(2,new String("two"));
System.out.println("Elements:"+h);
System.out.println(h.get(5));
h.remove(5);
System.out.println("Elements:"+h);
} }
Elements:{1=one, 2=two,
5=five}
five
Elements:{1=one, 2=two}
HashMap Example
import java.util.*;
class hashmapdemo{
public static void main(String args[]){
HashMap h=new HashMap();
h.put(1,new String("one"));
h.put(new String("hello"),new String("five"));
h.put(2,new String("two"));
h.put(new Character('a'),new String("kkkk"));
h.put(new Character('A'),new String("lll"));
h.put(new Character('b'), new String("jjjj"));
System.out.println("Elements:"+h);
} }
TreeMap Example
import java.util.*;
class hashmapdemo{
public static void main(String args[]){
TreeMap t=new TreeMap();
t.put(new Character('B'),new Integer(10));
t.put(new Character('C'),new Integer(20));
t.put(new Character('A'),new Integer(30));
System.out.println("Elements:"+t);
}}
Elements:{A=30, B=10, C=20}
Generics
Before Generics:
LinkedList l=new LinkedList();
l.add(new Integer(30));
Integer i=(Integer)l.get(0);
After Generics:
LinkedList<Integer> l=new LinkedList<Integer>();
l.add(new Integer(30));
Integer i=l.get(0);
LinkedList<Integer> l=new LinkedList<Integer>();
l.add(new Integer(30));
l.add(new String(“Hai”)); // Compile Time Errror
Integer i=l.get(0);
class one<F,S>{
F first;
S second;
one(F f,S s){
first= f; second=s;
}
}
class two<F,S,T> extends one<F,S>{
T third;
two(F f,S s,T t){
super(f,s);
third=t;
}
void show(){
System.out.println("First value:"+first);
System.out.println("First value:"+second);
System.out.println("First value:"+third);
}
}
public class demo {
StringTokenizer
import java.util.StringTokenizer;
public class MyClass1{
public static void main(String args[])throws
Exception{
String s="This, is an ,example! for string@
tokenizer";
StringTokenizer st=new StringTokenizer(s,",!@");
System.out.println(st.countTokens());
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
}
}
Output:
5
This
is an
example
for string
tokenizer
Date
import java.util.Date;
public class MyClass2 {
public static void main(String argv[]) {
Date d=new Date();
System.out.println(d);
System.out.println(d.getDate());
System.out.println(d.getDay());
System.out.println(d.getHours());
System.out.println(d.getMinutes());
System.out.println(d.getMonth());
System.out.println(d.getYear());
System.out.println(d.getSeconds());
}
}
Output:
Calendar
import java.util.Calendar;
public class MyClass3{
public static void main(String[] args) {
Calendar c=Calendar.getInstance();
System.out.println(c.get(Calendar.DATE));
System.out.println(c.get(Calendar.HOUR));
System.out.println(c.get(Calendar.SECOND));
System.out.println(c.get(Calendar.MINUTE));
System.out.println(c.get(Calendar.MONTH));
System.out.println(c.get(Calendar.YEAR));
}
}
Output:
30
4
53
48
7
2012