Enumeration or enums in java

Enumeration or enums :

Enumeration is just like a list which contains only constant type value. The items in the enumerated list are called enums.
Using enums, can help to reduce the bugs in from your code. The basic components of an enum are its constants.
Enums can be declared as their own class or as a class member while we can’t declare within any methods.

Example :
Here you will see enum declaration outside of a class.


package com.pkjavacode.com;

enum Fruit {
APPLE, MANGO, BANANA, ORANGE
}

public class EnumExample {

private Fruit name;

/**
* @auther pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println(fn.name);
}
}

Output :
APPLE

Note : in above example enum is declared outside of a class so you can not declare with private or protected modifier
so it is must be public or default.

You can also see an enum declaration within a class.

Example :


package com.pkjavacode.com;

public class EnumExample {
enum Fruit {
APPLE, MANGO, BANANA, ORANGE
}

private Fruit name;

/**
* @auther pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println(fn.name);
}
}

Output :
APPLE

Note : enums value can’t accept a String or int type.
Example :


enum Fruit {"apple", "mango", "banana"}

The above declaration of enum is not legal and if you will try to do that you will get syntax error by the compiler.

Declaring Constructors, Methods and Variables in an enum :

An enum is a kind of class so you can add constructor, method and variable in an enum just like you add these other
classes in java.

Example :


package com.pkjavacode.com;

enum Fruit {
APPLE(3), BANANA(12), MANGO(2);
// declare a constructor
Fruit(int number) {
this.number = number;
}

// declare an instance variable
private int number;

// declare a method
public int getNumber() {
return number;
}
}

public class EnumExample {

Fruit name;

/**
* @author pradeep
*/
public static void main(String[] args) {
// fruit name;
EnumExample fn = new EnumExample();
fn.name = Fruit.APPLE;
System.out.println("Number of apple are :" + fn.name.getNumber());
}
}

Output :
Number of apple are : 3

 

Advertisements

Criteria API in Hibernate

Criteria API in Hibernate :

The criteria is a simple api for fetching entities by creating Criterion object. With criteria you can apply number of condition with query such as using like wildcard, order by etc.

Here you will see create criteria operation through annotation  in Hibernate. To create criteria you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();

List employees=s.createCriteria(Employee.class).add(Restrictions.like("name", "p%")).addOrder(Order.asc("salary")).list();
t.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}
}
}

Delete operation in Hibernate

Delete operation in Hibernate :

Here you will see delete operation through annotation  in Hibernate. To create delete operation you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();
Employee emp = (Employee) s.load(Employee.class, 72);
s.delete(emp);
System.out.println("Record deleted successfully!");
t.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}
}
}

Output :

Hibernate: select employee0_.id as id0_0_, employee0_.name as name0_0_, employee0_.salary as salary0_0_ from employee employee0_ where employee0_.id=?
Record deleted successfully!
Hibernate: delete from employee where id=?

Update operation in Hibernate

Update operation in Hibernate :

Here you will see update operation through annotation  in Hibernate. To create update operation you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();
Employee e2= (Employee) s.get(Employee.class,72);
e2.setName("Rahul");
s.saveOrUpdate(e2);
System.out.println("Record updated successfully!");
t.commit();


} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}

}
}

Output :

Hibernate: select employee0_.id as id0_0_, employee0_.name as name0_0_, employee0_.salary as salary0_0_ from employee employee0_ where employee0_.id=?
Record updated successfully!
Hibernate: update employee set name=?, salary=? where id=?

Select operation in Hibernate

Select  operation in Hibernate :

Here you will see select operation through annotation  in Hibernate. To create select operation you have to create following java class.

  • Employee.java
  • HibernateTest.java

Employee.java :


package com.pkjavacode.com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author pradeep
*/
@Entity
@Table(name = "employee")
public class Employee implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "salary")
private Integer salary;

public Employee() {
}

public Employee(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getSalary() {
return salary;
}

public void setSalary(Integer salary) {
this.salary = salary;
}
}

HibernateTest.java :


package com.pkjavacode.com;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
*
* @author pradeep
*/
public class HibernateTest {

public static void main(String a[]) {

Session s = null;
try {
s = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
Transaction t = s.beginTransaction();
List<Employee> employees=s.createQuery("from Employee").list();
System.out.println("Id"+" "+"Name"+" "+"Salary");
System.out.println("----------------------------------");
for(Employee e2:employees) {
System.out.print(e2.getId()+" "+e2.getName()+" "+e2.getSalary()+"\n");
//System.out.print("\n"+e);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
s.close();
}
}
}

Output :

Hibernate: select employee0_.id as id0_, employee0_.name as name0_, employee0_.salary as salary0_ from employee employee0_
Id Name Salary
———————————-
72 Rahul 35000
73 Pradeep Yadav 45000
74 Amit 65000

BUILD SUCCESSFUL (total time: 3 seconds)

What is interface in java

interface in java :

 

An interface is a contract, which define what a class can do. With interface if you will use public then you will have to write this interface in a separate file.
if you will not use public with interface then you can write interface in any class file. You can use abstract keyword with interface it’s depends on you. In interface you can also declare variable but it’s static and final type only.
When you will implements interface in a class then all method of interface should be override with public keyword within that class.

Note : An adjective should be always taken to write an interface.

Syntax :


interface <interface_name>{
method1();
method2();
.
.
.
methodn();

final int VARIABLE1;
.
.
final int VARIABLEn;

}

 
Example :

 


interface MyInterface{
void display();
}

public class MyDemo implements MyInterface {
public void display(){
System.out.println("display method executing...");
}
public static void main(String[] args) {
MyInterface e=new MyDemo();
e.display();
}
}

&nbsp;

Hadoop deployment method

There are many methods used to deploy Hadoop,  few are as follows :

Standalone :

In this method, there are no daemons running, everythings runs as in a single JVM. This method is suitable for running MapReduce program during development and it is easy test, debug.

Pseudo-distributed :

The Hadoop daemon process runs on a local machine simulating a cluster on a small scale.

Fully distributed :

Here, Hadoop runs on a cluster of machines which provides production environment.