Wednesday, 21 November 2018
Tuesday, 20 November 2018
Java 9 Features
1-Jshell
2-JPMS(Java Platform module system)
3-JLink(Java Linker)
4-Process API updates
5-private methods inside interface
6-Try With Resource enhancement
7-Factory Methods to create unmodifiable Collection
8-Stream API Enhancement
9-Diamond (< >) Operator Enhancement
10-Http2 Client .
11-Safe Varargs Annotation
12-G1 Garbage Collector being default Garbage Collection.
7-Factory Methods to create unmodifiable Collection
8-Stream API Enhancement
9-Diamond (< >) Operator Enhancement
10-Http2 Client .
11-Safe Varargs Annotation
12-G1 Garbage Collector being default Garbage Collection.
Thursday, 15 November 2018
how to implement stack using dynamic array
public class StackWithArray {
private int capacity = 2;
private int stack[] = new int[capacity];
private int top = 0;
public void push(int data) {
if (size() == capacity)
expand();
stack[top] = data;
top++;
}
private void expand() {
int length = size();
int[] newStack = new int[capacity * 2];
System.arraycopy(stack, 0, newStack, 0, length);
stack = newStack;
capacity *= 2;
}
private int size() {
return top;
}
public int pop() {
int data = 0;
if (isEmpty()) {
System.out.println("stack is empty");
} else {
top--;
data = stack[top];
stack[top] = 0;
shrink();
}
return data;
}
public int peek() {
int data;
data = stack[top - 1];
return data;
}
private void shrink() {
int length = size();
if (length <= (capacity / 2) / 2)
capacity = capacity / 2;
int newStack[] = new int[capacity];
System.arraycopy(stack, 0, newStack, 0, length);
stack = newStack;
}
public boolean isEmpty() {
return top <= 0;
}
public void displayData() {
for (int n : stack) {
System.out.print(n + " ");
}
System.out.println();
}
public static void main(String[] args) {
StackWithArray st = new StackWithArray();
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.pop();
st.displayData();
st.pop();
st.displayData();
st.pop();
st.displayData();
st.pop();
st.displayData();
}
}
Wednesday, 14 November 2018
How To implement Stack in Java
public class MyStack {
int data[]=new int[5];
int top=0;
static int size=0;
public void push(int value) {
data [top]=value;
top++;
size++;
}
int pop() {
int d=0;
for(int i=size-1; i>=0; i--) {
System.out.println(data[i]);
d=data[i];
data[i]=0;
}
return d;
}
public static void main(String[] args) {
MyStack st=new MyStack();
st.push(1);
st.push(5);
st.push(2);
st.push(0);
st.push(1);
st.pop();
}
}
How to implement custom Queue in Java
public class MyQueue {
public static void main(String[] args) {
Queue q = new Queue();
q.enQueue(5);
q.enQueue(8);
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.show();
}
}
class Queue {
int size;
int[] queue;
int front;
int rear;
Queue() {
size = 0;
queue = new int[5];
}
public void enQueue(int data) {
queue[rear] = data;
rear = rear + 1;
size = size + 1;
}
public void show() {
for (int i = 0; i < queue.length; i++) {
System.out.println(queue[i]);
}
}
}
public static void main(String[] args) {
Queue q = new Queue();
q.enQueue(5);
q.enQueue(8);
q.enQueue(1);
q.enQueue(2);
q.enQueue(3);
q.show();
}
}
class Queue {
int size;
int[] queue;
int front;
int rear;
Queue() {
size = 0;
queue = new int[5];
}
public void enQueue(int data) {
queue[rear] = data;
rear = rear + 1;
size = size + 1;
}
public void show() {
for (int i = 0; i < queue.length; i++) {
System.out.println(queue[i]);
}
}
}
Wednesday, 31 October 2018
how to create doubly circular linked list in java
package hello;
public class MyLinkedList<E> {
private Node<E> first;
private Node<E> last;
int size;
public MyLinkedList() {
size = 0;
}
public int size() {
return size;
}
public static class Node<E> {
Node<E> next;
Node<E> pre;
E elelement;
public Node(Node p, E e, Node n) {
this.elelement = e;
this.next = n;
this.pre = p;
}
public E getElelement() {
return elelement;
}
public void setElelement(E elelement) {
this.elelement = elelement;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public Node<E> getPre() {
return pre;
}
public void setPre(Node<E> pre) {
this.pre = pre;
}
}
public void printForward() {
Node n = first;
for (int i = 0; i < size; i++) {
if (n.getPre() == null) {
System.out.print(n.getPre() + "::" + n.getElelement());
n = n.next;
} else if (n.getNext() == null) {
System.out.println("-->" + n.getElelement() + "::" + n.getNext());
} else {
System.out.print("-->" + n.getElelement() + "::");
n = n.next;
}
}
}
public void printBackward() {
System.out.println();
Node n = last;
for (int i = 0; i < size; i++) {
if (n.getNext() == null) {
System.out.print(n.getNext() + "-->::" + n.getElelement());
n = n.pre;
} else if (n.getPre() == null) {
System.out.println("-->" + n.getElelement() + "::-->" + n.getPre());
} else {
System.out.print("-->" + n.getElelement() + "::");
n = n.pre;
}
}
}
public void add(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null) {
first = newNode;
} else {
l.next = newNode;
}
size++;
}
public void addCircular(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null) {
first = newNode;
} else {
l.next = newNode;
newNode.next = first.pre;
first.pre = l.next;
}
size++;
}
public static void main(String[] args) {
MyLinkedList<Integer> iList = new MyLinkedList<Integer>();
iList.add(1);
iList.add(2);
iList.add(3);
iList.add(4);
iList.printForward();
iList.printBackward();
System.out.println("--------------------------------------------------------------");
iList.addCircular(1);
iList.addCircular(2);
iList.addCircular(3);
iList.addCircular(4);
iList.printForward();
// System.out.println(iList.size);
}
}
public class MyLinkedList<E> {
private Node<E> first;
private Node<E> last;
int size;
public MyLinkedList() {
size = 0;
}
public int size() {
return size;
}
public static class Node<E> {
Node<E> next;
Node<E> pre;
E elelement;
public Node(Node p, E e, Node n) {
this.elelement = e;
this.next = n;
this.pre = p;
}
public E getElelement() {
return elelement;
}
public void setElelement(E elelement) {
this.elelement = elelement;
}
public Node<E> getNext() {
return next;
}
public void setNext(Node<E> next) {
this.next = next;
}
public Node<E> getPre() {
return pre;
}
public void setPre(Node<E> pre) {
this.pre = pre;
}
}
public void printForward() {
Node n = first;
for (int i = 0; i < size; i++) {
if (n.getPre() == null) {
System.out.print(n.getPre() + "::" + n.getElelement());
n = n.next;
} else if (n.getNext() == null) {
System.out.println("-->" + n.getElelement() + "::" + n.getNext());
} else {
System.out.print("-->" + n.getElelement() + "::");
n = n.next;
}
}
}
public void printBackward() {
System.out.println();
Node n = last;
for (int i = 0; i < size; i++) {
if (n.getNext() == null) {
System.out.print(n.getNext() + "-->::" + n.getElelement());
n = n.pre;
} else if (n.getPre() == null) {
System.out.println("-->" + n.getElelement() + "::-->" + n.getPre());
} else {
System.out.print("-->" + n.getElelement() + "::");
n = n.pre;
}
}
}
public void add(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null) {
first = newNode;
} else {
l.next = newNode;
}
size++;
}
public void addCircular(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null) {
first = newNode;
} else {
l.next = newNode;
newNode.next = first.pre;
first.pre = l.next;
}
size++;
}
public static void main(String[] args) {
MyLinkedList<Integer> iList = new MyLinkedList<Integer>();
iList.add(1);
iList.add(2);
iList.add(3);
iList.add(4);
iList.printForward();
iList.printBackward();
System.out.println("--------------------------------------------------------------");
iList.addCircular(1);
iList.addCircular(2);
iList.addCircular(3);
iList.addCircular(4);
iList.printForward();
// System.out.println(iList.size);
}
}
Thursday, 16 October 2014
JPA2 Without Toplink Implemation
Create Model Class
package myPack;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Emp
{
@Id
int id;
String name,job;
int salary;
public Emp() {
super();
}
public Emp(int id, String name, String job, int salary) {
super();
this.id = id;
this.name = name;
this.job = job;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
package myPack;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Emp
{
@Id
int id;
String name,job;
int salary;
public Emp() {
super();
}
public Emp(int id, String name, String job, int salary) {
super();
this.id = id;
this.name = name;
this.job = job;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
Create Factory Class
package myPack;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class MyFactory
{
static EntityManagerFactory EMF;
static
{
EMF=Persistence.createEntityManagerFactory("JPA1withTopLinkPU");
}
public static EntityManager getManager()
{
return EMF.createEntityManager();
}
}
Create Persist Class
package myPack;
import javax.persistence.EntityManager;
import javax.persistence.*;
public class PersistDemo {
public static void main(String[] args)
{
EntityManager m=MyFactory.getManager();
Emp e1=new Emp(8,"Bharat","Developer",15000);
Emp e2=new Emp(9,"Ankit","Developer",17000);
System.out.println("Persisting Entities...........");
EntityTransaction t=m.getTransaction();
t.begin();
m.persist(e1);
m.persist(e2);
t.commit();
m.close();
System.out.println("SUCCESSFULLY PERISTED");
}
}
Create Persistence Configuration Xml File
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="JPA1withTopLinkPU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<class>myPack.Emp</class>
<properties>
<property name = "toplink.jdbc.driver" value = "oracle.jdbc.driver.OracleDriver"/>
<property name = "toplink.jdbc.url" value = "jdbc:oracle:thin:@localhost:1521:xe"/>
<property name = "toplink.jdbc.user" value = "system"/>
<property name = "toplink.jdbc.password" value = "oracle"/>
</properties>
</persistence-unit>
</persistence>
Create FetchTest Class
package myPack;
import java.util.Scanner;
import javax.persistence.EntityManager;
public class FetchTest {
public static void main(String[] args) {
EntityManager m=MyFactory.getManager();
Scanner in=new Scanner(System.in);
System.out.println("Enter your id:");
int id=in.nextInt();in.nextLine();
System.out.println("Find method used........");
Emp e1=m.find(Emp.class, id);
System.out.println("Details of fetchd entity using data memeber:");
System.out.println(e1.name+"\t"+e1.job+"\t"+e1.salary);
System.out.println("Details of fetched entity getter Methods");
System.out.println(e1.getName()+"\t"+e1.getJob()+"\t"+e1.getSalary());
System.out.println("GetReference method used......");
Emp e=m.getReference(Emp.class,id);
System.out.println("Details of fetchd entity using data memeber:");
System.out.println(e.name+"\t"+e.job+"\t"+e.salary);
System.out.println("Details of fetched entity getter Methods");
System.out.println(e.getName()+"\t"+e.getJob()+"\t"+e.getSalary());
m.close();
}
}
Struts2 Hibernate Integration java programmer can use it in simplified manner
Create A Model Class A class:
package Model;
public class Emp
{
String name,job;
int salary;
int id;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(String name, String job, int salary) {
super();
this.name = name;
this.job = job;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Create Hibernate Session Factory
package HibernateUtile;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class MySesssionFactory
{
static SessionFactory factory;
static
{
Configuration cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}
public static Session getSession()
{
return factory.openSession();
}
}
Create struts.xml file
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="add" class="View.ActionAaAview">
<result name="success">/Welcome.jsp</result>
<!--<result name="failure">/Retrive.jsp</result>-->
</action>
</package>
</struts>
Copy bellow text and paste in web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>f1</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Create index.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="add" >
<s:textfield name="Emp.name" label="Name"/>
<s:textfield name="Emp.job" label="Job"/>
<s:textfield name="Emp.salary" label="Salary"/>
<s:token/>
<s:submit name="Register"/>
<s:reset name="Reset"/>
</s:form>
Create welcome.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
Welcome<s:property value="Emp.name"/>
</s:form>
Create retrive.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="add" method="post">
<s:textfield name="name" label="Name"/>
<s:textfield name="job" label="Job"/>
<s:textfield name="salary" label="Salary"/>
<s:submit name="Register"/>
<s:reset name="Reset"/>
<s:textfield />
</s:form>
package Model;
public class Emp
{
String name,job;
int salary;
int id;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(String name, String job, int salary) {
super();
this.name = name;
this.job = job;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Create Hibernate Session Factory
package HibernateUtile;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class MySesssionFactory
{
static SessionFactory factory;
static
{
Configuration cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}
public static Session getSession()
{
return factory.openSession();
}
}
Create Dao Class
package Dao;
import org.hibernate.Session;
import org.hibernate.Transaction;
import HibernateUtile.MySesssionFactory;
import Model.Emp;
public class EmpDao
{
public void insert(Emp emp)
{
Session session=MySesssionFactory.getSession();
Transaction tr=session.beginTransaction();
session.save(emp);
tr.commit();
session.close();
}
}
Create Hibernate Configuration
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">system</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.password">oracle</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="show_sql">true</property>
<mapping resource="Hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Create Hibernate Mapping File
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-mapping>
<class name="Model.Emp">
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name"/>
<property name="job"/>
<property name="salary" type="int"/>
</class>
</hibernate-mapping>
Create Controller Page
package View;
import Dao.EMpDao;
import Model.Emp;
public class ActionAaAview
{
private Emp e;
public Emp getE() {
return e;
}
public void setE(Emp e) {
this.e = e;
}
public String execute()
{
EmpDao ctr=new EmpDao();
ctr.insert(e);
return "success";
}
}
Create struts.xml file
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="add" class="View.ActionAaAview">
<result name="success">/Welcome.jsp</result>
<!--<result name="failure">/Retrive.jsp</result>-->
</action>
</package>
</struts>
Copy bellow text and paste in web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>f1</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
Create index.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="add" >
<s:textfield name="Emp.name" label="Name"/>
<s:textfield name="Emp.job" label="Job"/>
<s:textfield name="Emp.salary" label="Salary"/>
<s:token/>
<s:submit name="Register"/>
<s:reset name="Reset"/>
</s:form>
Create welcome.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
Welcome<s:property value="Emp.name"/>
</s:form>
Create retrive.jsp
<%@taglib prefix="s" uri="/struts-tags"%>
<s:form action="add" method="post">
<s:textfield name="name" label="Name"/>
<s:textfield name="job" label="Job"/>
<s:textfield name="salary" label="Salary"/>
<s:submit name="Register"/>
<s:reset name="Reset"/>
<s:textfield />
</s:form>
Subscribe to:
Posts (Atom)


