Monday, January 16, 2012

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.

Sunday, January 15, 2012

Sorting and Searching using in build Arrays methods


import java.util.Arrays;
class Sorting
{
static void display(int array[])  //Static method for Displaying
{
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]+" ");
}
System.out.println("");
}

public static void main(String args[])
{
int array[]=new int[10];

for(int i=0;i<10;i++)
array[i]=-3*i; //assigning some values in single dimensional array

System.out.println("Original contents:");  //Displaying original Contents
display(array);

Arrays.sort(array); //calling sort method to sort the contents
System.out.println("Sorted:");
display(array);

Arrays.fill(array,2,6,-1); //replacing the 0th position to 5th position with "-1"
System.out.println("After fil():");
display(array);

Arrays.sort(array); //sorting after inserting -1
System.out.println("After sorting:");
display(array);

System.out.println("The value -9 is at location"); //Searching the "-9" in the array by calling binary Search method
int index=Arrays.binarySearch(array,-9);
System.out.println(index);

}
}

Arrays


1. Two Dimentional Array Example

class ArrayDemo
{
public static void main(String args[])
{
int[]num={1,2,3,4,5}; //Single Dimensional Array
System.out.println("Array Length:" + num.length);

int k=0;
int [][] twoD=new int[4][5]; //Two Dimensional array (even columns)
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
twoD[i][j]=k;    //Storing 1 to 19 in two dimentional array
k++;
}

}

for(int i=0;i<4;i++)      //Displaying the elements from two dimentional array
{
for(int j=0;j<5;j++)
{
System.out.print(twoD[i][j]+"   ");
}
System.out.println();

}

}
}

Output:

Array Length:5
0   1   2   3   4
5   6   7   8   9
10   11   12   13   14
15   16   17   18   19

2. Two Dimensional (Uneven column) Array Example

public class Uneven
{
public static void main(String args[])
{
int [][] twoD=new int[4][];      //Two dimensional with Uneven columns array declaration
int k=0;
twoD[0]=new int[1];
twoD[1]=new int[2];
twoD[2]=new int[3];
twoD[3]=new int[4];
for(int i=0;i<4;i++)
{
for(int j=0;j<i+1;j++)
{
twoD[i][j]=k;
k++;
}
}

for(int i=0;i<4;i++)
{
for(int j=0;j<i+1;j++)
{
System.out.print(twoD[i][j]+" ");
}
System.out.println();
}
}
}

Output:

0
1 2
3 4 5
6 7 8 9