Monday, January 16, 2012

ArrayDeque - used like a Stack

import java.util.*;
class ArrayDequeDemo
{
public static void main(String args[])
{
//create ArrayDeque
ArrayDeque<String> adq=new ArrayDeque<String>();

//using ArrayDeque like stack.
adq.push("A");
adq.push("B");
adq.push("D");
adq.push("E");
adq.push("C");
adq.push("F");
System.out.println(adq);

//Popping the elements from ArrayDeque
System.out.println("Popping the stack");
while(adq.peek()!=null)  //  returns null if this deque is empty.
{
System.out.println(adq.pop() +"- popped ");
}
System.out.println(adq);
}

}

Output:
[F, C, E, D, B, A]
Popping the stack
F- popped
C- popped
E- popped
D- popped
B- popped
A- popped
[]

Note:

  • ArrayDeque implements Deque

TreeSet


import java.util.*;
class TreeSetDemo
{
public static void main(String args[])
{
//create TreeSet
TreeSet<String> hs=new TreeSet<String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);

//Iterator to display elements
                System.out.println("Displaying contents of hs using for each: ");
Iterator itr=hs.iterator();
while(itr.hasNext())
{
Object element=itr.next();
System.out.print(element + "-");
}
System.out.println();

//For each, alternative to iterator
System.out.println("Displaying contents of hs using for each: ");
for(String s:hs)
System.out.print(s + " ");
System.out.println();

//--------------------------------------------------------
//adding a to z in TreeSet
System.out.println();
LinkedHashSet<Character> h=new LinkedHashSet<Character>();
for(int i=97;i<122;i++)
h.add((char)i);
h.add('b');
System.out.println(h);

//Iterator to display elements
                System.out.println("Displaying contents of hs using for each: ");
Iterator itr1=h.iterator();
while(itr1.hasNext())
{
Object element=itr1.next();
System.out.print(element + "-");
}
System.out.println();

//For each, alternative to iterator
System.out.println("Displaying contents of h using for each: ");
for(Character s1:h)
System.out.print(s1 + " ");
System.out.println();

}
}


Output:


[A, B, C, D, E, F]
Displaying contents of hs using Iterator:
A-B-C-D-E-F-
Displaying contents of hs using for each:
A B C D E F

[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y]
Displaying contents of h using Iterator:
a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-
Displaying contents of h using for each:
a b c d e f g h i j k l m n o p q r s t u v w x y



Note:
  • LinkedHashSet guarantee the elements in sorted, ascending order.
  • In LinkedHashSet ListIterator cannot be used.
  • LinkedHashSet implements NavigableSet interface.


LinkedHashSet

import java.util.*;
class LinkedHashSetDemo
{
public static void main(String args[])
{
//create HashSet
LinkedHashSet<String> hs=new LinkedHashSet<String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);

//Iterator to display elements
System.out.println("Displaying contents of hs using Iterator: ");
Iterator itr=hs.iterator();
while(itr.hasNext())
{
Object element=itr.next();
System.out.print(element + "-");
}
System.out.println();

//For each, alternative to iterator
System.out.println("Displaying contents of hs using for each: ");
for(String s:hs)
System.out.print(s + " ");
System.out.println();

//--------------------------------------------------------
//adding a to z in HashSet
System.out.println();
LinkedHashSet<Character> h=new LinkedHashSet<Character>();
for(int i=97;i<122;i++)
h.add((char)i);
h.add('b');
System.out.println(h);

//Iterator to display elements
System.out.println("Displaying contents of h using Iterator: ");
Iterator itr1=h.iterator();
while(itr1.hasNext())
{
Object element=itr1.next();
System.out.print(element + "-");
}
System.out.println();

//For each, alternative to iterator
System.out.println("Displaying contents of h using for each: ");
for(Character s1:h)
System.out.print(s1 + " ");
System.out.println();
}
}


Output:
[B, A, D, E, C, F]
Displaying contents of hs using Iterator:
B-A-D-E-C-F-
Displaying contents of hs using for each:
B A D E C F

[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y]
Displaying contents of h using Iterator:
a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-
Displaying contents of h using for each:
a b c d e f g h i j k l m n o p q r s t u v w x y


Note:
  • LinkedHashSet guarantee the order of its elements in which they were inserted.
  • In LinkedHashSet ListIterator cannot be used.
  • LinkedHashSet implements Set interface.

HashSet

import java.util.*;
class HashSetDemo
{
public static void main(String args[])
{
//create HashSet
HashSet<String> hs=new HashSet<String>();
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);

//Iterator to display elements
System.out.print("Displaying contents of hs using Iterator: ");
Iterator itr=hs.iterator();
while(itr.hasNext())
{
Object element=itr.next();
System.out.print(element + " ");
}
System.out.println();

//For each, alternative to iterator
System.out.print("Displaying contents of hs using for each: ");
for(String s:hs)
System.out.print(s + " ");
System.out.println();
//--------------------------------------------------------
//adding a to z in HashSet
System.out.println();
HashSet<Character> h=new HashSet<Character>();
for(int i=97;i<122;i++)
h.add((char)i);
h.add('b');
System.out.println(h);

//Iterator to display elements
System.out.println("Displaying contents of h using Iterator: ");
Iterator itr1=h.iterator();
while(itr1.hasNext())
{
Object element=itr1.next();
System.out.print(element + "-");
}
System.out.println();

//For each, alternative to iterator
System.out.println("Displaying contents of h using for each: ");
for(Character s1:h)
System.out.print(s1 + " ");
System.out.println();

}
}

Output:
[D, E, F, A, B, C]
Displaying contents of hs using Iterator: D E F A B C
Displaying contents of hs using for each: D E F A B C

[f, g, d, e, b, c, a, n, o, l, m, j, k, h, i, w, v, u, t, s, r, q, p, y, x]
Displaying contents of h using Iterator:
f-g-d-e-b-c-a-n-o-l-m-j-k-h-i-w-v-u-t-s-r-q-p-y-x-
Displaying contents of h using for each:
f g d e b c a n o l m j k h i w v u t s r q p y x

Note:
  • HashSet does not guarantee the order of its elements
  • In HashSet ListIterator cannot be used.
  • HashSet implements Set Interface.



Linked List

import java.util.*;
class LinkedListDemo
{
public static void main(String args[])
{
// create a linked list
LinkedList<String> ll = new LinkedList<String>();

// add elements to the linked list
ll.add("F");
ll.add("B");
ll.add("D");
ll.add("E");
ll.add("C");
System.out.println("Original contents of ll: " + ll);

//adding first and last element in linkedlist
ll.addLast("Z");
ll.addFirst("A");
ll.add(1, "A2");
System.out.println("contents of ll after adding first and last: " + ll);

// remove elements from the linked list
ll.remove("F");
ll.remove(2);
System.out.println("Contents of ll after deletion: " + ll);

// remove first and last elements
ll.removeFirst();
ll.removeLast();
System.out.println("ll after deleting first and last: " + ll);

// get and set a value
Object val = ll.get(2);
ll.set(2, (String) val + " Changed");
System.out.println("ll after change: " + ll);

//Iterator to display elements
System.out.print("Displaying contents of ll using Iterator: ");
Iterator itr = ll.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();

//For each, alternative to iterator
System.out.print("Displaying contents of ll using for each: ");
for(String s:ll)
{
System.out.print(s + " ");
}
System.out.println();

// modify objects being iterated
ListIterator litr = ll.listIterator();
while(litr.hasNext())
{
Object element = litr.next();
litr.set(element + "+");
}

System.out.print("Modified contents of ll: ");
itr = ll.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();

// now, display the list backwards
System.out.print("Modified list backwards: ");
while(litr.hasPrevious())
{
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();
}
}

Output:
Original contents of ll: [F, B, D, E, C]
contents of ll after adding first and last: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
ll after deleting first and last: [A2, D, E, C]
ll after change: [A2, D, E Changed, C]
Displaying contents of ll using Iterator: A2 D E Changed C
Displaying contents of ll using for each: A2 D E Changed C
Modified contents of ll: A2+ D+ E Changed+ C+
Modified list backwards: C+ E Changed+ D+ A2+

Note:
LinkedList implements List, Deque and Queue.

Converting an ArrayList into an array.


//Obtaining an Array from an ArrayList

import java.util.*;
class ArrayListToArray
{
public static void main(String args[])
{
// Create an array list
ArrayList<Integer> al = new ArrayList<Integer>();

// Add elements to the array list
al.add(new Integer(1));
al.add(new Integer(2));
al.add(new Integer(3));
al.add(new Integer(4));
System.out.println("Contents of al: " + al);

// get array
Object ia[] = al.toArray();
int sum = 0;

//Display using array
System.out.println("Display using array");
for(int i=0;i<ia.length;i++)
{
System.out.println(ia[i]);
}

// sum the array
for(int i=0; i<ia.length; i++)
sum += ((Integer) ia[i]).intValue();
System.out.println("Sum is: " + sum);
}
}

Output:

Contents of al: [1, 2, 3, 4]
Display using array
1
2
3
4
Sum is: 10


Note:
Reasons to convert ArrayList to Array:
  • To obtain faster processing times for certain operations.
  • To pass an array to a method that is not overloaded to accept a collection.
  • To integrate collection-based code with legacy code that does not understand collections.

Array List


import java.util.*;
class ArrayListDemo
{
public static void main(String args[])
{
// create an array list
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " +
al.size());

// add elements to the array list
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());

// display the array list
System.out.println("Contents of al: " + al);

// Remove elements from the array list
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " +
al.size());
System.out.println("Contents of al: " + al);

//Iterator to display elements
System.out.print("Displaying contents of al using Iterator: ");
Iterator itr = al.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();

//For each, alternative to iterator
System.out.print("Displaying contents of al using for each: ");
for(String s:al)
{
System.out.print(s + " ");
}
System.out.println();

// modify objects being iterated
ListIterator litr = al.listIterator();
while(litr.hasNext())
{
Object element = litr.next();
litr.set(element + "+");
}

System.out.print("Modified contents of al: ");
itr = al.iterator();
while(itr.hasNext())
{
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();

// now, display the list backwards
System.out.print("Modified list backwards: ");
while(litr.hasPrevious())
{
Object element = litr.previous();
System.out.print(element + " ");
}
System.out.println();


}
}


Output:

Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
Displaying contents of al using Iterator: C A2 E B D
Displaying contents of al using for each: C A2 E B D
Modified contents of al: C+ A2+ E+ B+ D+
Modified list backwards: D+ B+ E+ A2+ C+


Note:
  • ArrayList implements only List interface.