Unit1 Hadoop
Unit1 Hadoop
UNIT 1
Linked List:
Linked Lists are a very common way of storing arrays of data. The major benefit of linked
lists is that you do not specify a fixed size for your list. The more elements you add to the
chain, the bigger the chain gets.
There is more than one type of a linked list, although for the purpose of this tutorial, we'll
stick to singly linked lists (the simplest one). If for example you want a doubly linked list
instead, very few simple modifications will give you what you're looking for. Many data
structures (e.g. Stacks, Queues, Binary Trees) are often implemented using the concept of
linked lists. Some different types of linked lists are shown below.
The following java program demonstrate about implementation of linked list in java
import java.util.Scanner
/* Class Node */
class Node
{
protected int data;
protected Node link;
public Node()
{ link = null;
data = 0;
}
/* Class linkedList */
class linkedList
/* Constructor */
public linkedList()
{ start = null;
end = null;
size = 0;
}
public boolean isEmpty()
{ return start == null;
}
/* Function to get size of list */
public int getSize()
{ return size;
}
/* Function to insert an element at begining */
public void insertAtStart(int val)
{ Node nptr = new Node(val, null);
size++ ;
if(start == null)
{ start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
public void insertAtEnd(int val)
{ Node nptr = new Node(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
public void insertAtPos(int val , int pos)
{
Node nptr = new Node(val, null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{ Node tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{ start = start.getLink();
size--;
return ;
}
if (pos == size)
{ Node s = start;
Node t = start;
while (s != end)
{ t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{ Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
/* Class SinglyLinkedList */
public class SinglyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test\n");
char ch;
/* Perform list operations */
do
{
System.out.println("\nSingly Linked List Operations\n");
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position\n");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" \n");
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* Display List */
list.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);
} while (ch != 'N'|| ch != 'n');
}
}
Stacks
A stack is a data structure that allows data to be inserted (a 'push' operation), and
removed (a 'pop' operation). Many stacks also support a read ahead (a 'peek' operation),
which reads data without removing it. A stack is a LIFO-queue, meaning that the last data
to be inserted will be the first data to be removed.
Queue collection implements First-In Fist-Out behavior on items stored within it.
import java.util.*;
public class QueueDemo {
static String newLine = System.getProperty("line.separator");
public static void main(String[] args) {
System.out.println(newLine + "Queue in Java" + newLine);
System.out.println("-----------------------" + newLine);
System.out.println("Adding items to the Queue" + newLine);
//Creating queue would require you to create instannce of LinkedList and assign
//it to Queue
//Object. You cannot create an instance of Queue as it is abstract
Queue queue = new LinkedList();
queue.add("Javascript");
queue.add("HTML5");
queue.add("Hadoop");
//.element() returns the current element in the queue, here when "java" is removed
//the next most top element is .NET, so .NET would be printed.
System.out.println("retrieve element: " + queue.element() + newLine);
//.peek() just returns the current element in the queue, null if empty
//Here it will print Javascript as .NET is removed above
System.out.println("retrieve element, null is empty " + queue.peek() + newLine);
}
}
Sets
Set is a collection of elements. In Java HashSet class is used for maintaining Set type data.
Maps
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.
Several methods throw a NoSuchElementException when no items exist in the
invoking map.
Example:
Map has its implementation in various classes like HashMap. Following is the example to
explain map functionality:
import java.util.*;
public class CollectionsDemo {
public static void main(String[] args) {
Map m1 = new HashMap();
m1.put("Zara", "8");
m1.put("Mahnaz", "31");
m1.put("Ayan", "12");
m1.put("Daisy", "14");
System.out.println();
System.out.println(" Map Elements");
System.out.print("\t" + m1);
}
}
Map Elements
{Mahnaz=31, Ayan=12, Daisy=14, Zara=8}
2 Generics:
It would be nice if we could write a single sort method that could sort the elements in an
Integer array, a String array or an array of any type that supports ordering.
Java Generic methods and generic classes enable programmers to specify, with a
single method declaration, a set of related methods or, with a single class declaration, a
set of related types, respectively.
Generics also provide compile-time type safety that allows programmers to catch
invalid types at compile time.
Using Java Generic concept, we might write a generic method for sorting an array of
objects, then invoke the generic method with Integer arrays, Double arrays, String arrays
and so on, to sort the array elements.
Generic
Methods:
You can write a single generic method declaration that can be called with arguments of
different types. Based on the types of the arguments passed to the generic method, the
compiler handles each method call appropriately. Following are the rules to define Generic
Methods:
All generic method declarations have a type parameter section delimited by angle
brackets (< and >) that precedes the method's return type ( < E > in the next
example).
Each type parameter section contains one or more type parameters separated by
commas. A type parameter, also known as a type variable, is an identifier that
specifies a generic type name.
The type parameters can be used to declare the return type and act as placeholders
for the types of the arguments passed to the generic method, which are known as
actual type arguments.
A generic method's body is declared like that of any other method. Note that type
parameters can represent only reference types, not primitive types (like int, double
and char).
Example
:
Following example illustrates how we can print array of different type using a single
Generic method:
Bounded Type
Parameters:
There may be times when you'll want to restrict the kinds of types that are allowed to be
passed to a type parameter. For example, a method that operates on numbers might only
want to accept instances of Number or its subclasses. This is what bounded type
parameters are for.
To declare a bounded type parameter, list the type parameter's name, followed by the
extends keyword, followed by its upper bound.
Example
:
Following example illustrates how extends is used in a general sense to mean either
"extends" (as in classes) or "implements" (as in interfaces). This example is Generic
method to return the largest of three Comparable objects:
maximum of 3, 4 and 5 is 5
Generic
Classes:
A generic class declaration looks like a non-generic class declaration, except that the class
name is followed by a type parameter section.
As with generic methods, the type parameter section of a generic class can have one or
more type parameters separated by commas. These classes are known as parameterized
classes or parameterized types because they accept one or more parameters.
Example
:
Following example illustrates how we can define a generic class:
private T t;
integerBox.add(new Integer(10));
stringBox.add(new String("Hello World"));
3 Wrapper Classes
All the wrapper classes (Integer, Long, Byte, Double, Float, Short) are subclasses of the
abstract classNumber. The object of the wrapper class contains or wraps its respective
primitive data type.
Wrapper class in java provides the mechanism to convert primitive into object and object
into primitive.
Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and
object into primitive automatically. The automatic conversion of primitive into object is
known and autoboxing and vice-versa unboxing.
One of the eight classes of java.lang package are known as wrapper class in java. The list of
eight wrapper classes are given below:
Boolean Boolean
Char Character
Byte Byte
Short Short
Int Integer
Long Long
Float Float
Double Double
20 20 20
333
4 Concept of Serialization
Java provides a mechanism, called object serialization where an object can be represented
as a sequence of bytes that includes the object's data as well as information about the
object's type and the types of data stored in the object.
After a serialized object has been written into a file, it can be read from the file and
deserialized that is, the type information and bytes that represent the object and its data
can be used to recreate the object in memory.
Most impressive is that the entire process is JVM independent, meaning an object can be
serialized on one platform and deserialized on an entirely different platform.
The ObjectOutputStream class contains many write methods for writing various data types,
but one method in particular stands out:
The above method serializes an Object and sends it to the output stream. Similarly, the
ObjectInputStream class contains the following method for deserializing an object:
This method retrieves the next Object out of the stream and deserializes it. The return
value is Object, so you will need to cast it to its appropriate data type.
To demonstrate how serialization works in Java, I am going to use the Student class that
we discussed early on in the book. Suppose that we have the following Student class,
which implements the Serializable interface:
public class Student implements java.io.Serializable
{
public String name;
public String address;
public transient int SSN;
public int number;
Notice that for a class to be serialized successfully, two conditions must be met:
All of the fields in the class must be serializable. If a field is not serializable, it must
be marked transient.
If you are curious to know if a Java Standard Class is serializable or not, check the
documentation for the class. The test is simple: If the class implements
java.io.Serializable, then it is serializable; otherwise, it's not.
Serializing an
Object:
When the program is done executing, a file named Student.ser is created. The program
does not generate any output, but study the code and try to determine what the program
is doing.
Note: When serializing an object to a file, the standard convention in Java is to give the
file a .ser extension.
import java.io.*;
public class SerializeDemo
{
public static void main(String [] args)
{
Student e = new Student();
e.name = "srinivas";
e.address = "ibm vijayawada";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("/tmp/Student.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/Student.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
Deserializing an Object:
The following DeserializeDemo program deserializes the Student object created in the
SerializeDemo program. Study the program and try to determine its output:
import java.io.*;
public class DeserializeDemo
{
public static void main(String [] args)
{
Student e = null;
try
{
FileInputStream fileIn = new FileInputStream("/tmp/Student.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Student) in.readObject();
in.close();
fileIn.close();
}catch(IOException i)
{
i.printStackTrace();
return;
}catch(ClassNotFoundException c)
{
System.out.println("Student class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Student...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
Deserialized Student...
Name: srinivas
Address:ibm,vijayawada
SSN: 0
Number:101
Here are following important points to be noted:
The value of the SSN field was 11122333 when the object was serialized, but
because the field is transient, this value was not sent to the output stream. The SSN
field of the deserialized Student object is 0.