Tags » Java Tutorial

Advantages of Hibernate Framework

Advantages of  Hibernate Framework :

  • Hibernate has capability to generate primary keys automatically while storing the records into database.
  • Hibernate provide automatic table creation facility.
  • 163 more words
Java

What is Hibernate Framework

Hibernate Framework :

Hibernate framework is an ORM(Object Relational Mapping) tools which is used to map java class with database table name and class variable with database table’s column. 49 more words

Java

Use comparable and sort employee list based on id

Use comparable and sort employee list based on id  :


package com,pkjavacode.com;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
*
* @author pradeep
*/
public class Emp implements Comparable<Emp> {

private int id;
private String name;

public Emp(int id, String name) {
this.id = id;
this.name = name;
}

/**
* @return the id
*/
public int getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

public String toString() {
return "Employee details :" + "ID" + id + " Name :" + name;
}

@Override
public int compareTo(Emp e) {
return this.id - e.id;
}

public static void main(String[] args) {
Emp e1 = new Emp(101, "Pradeep");
Emp e2 = new Emp(107, "Ajeet");
Emp e3 = new Emp(103, "Amit");
Emp e4 = new Emp(104, "Rajesh");
Emp e5 = new Emp(105, "Bharat");
List<Emp> employees = Arrays.asList(e1, e2, e3, e4, e5);
System.out.println("Unsorted data :");
for (Emp employee : employees) {
System.out.println("Employee : " + employee.id + " Name: " + employee.name);
}
Collections.sort(employees);
System.out.println("sorted based on id :");
for (Emp employee : employees) {
System.out.println("Employee : " + employee.id + " Name: " + employee.name);
}
}
}

… 37 more words
Java

write a program to reverse string in java

program to reverse string  :


package com.pkjavacode.com;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ComparableExample {

/**
* @auther pradeep
*/
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.print("Enter String to reverse :");
String str = br.readLine();
String rev = "";
int n = str.length();
for (int i = n - 1; i >= 0; i--) {
rev = rev + str.charAt(i);

}
System.out.print("String after reverse are :" + rev);
}

catch (Exception e) {
System.out.println("" + e.getMessage());
}
}

}

… 11 more words
Java

Find fibonacci series in java

Fibonacci Series :


package com.pkjavacode.com;

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

static int fib(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return (fib(n - 1) + fib(n - 2));
}
}

public static void main(String[] args) {
int n = 5;
System.out.println("Fibonacci Series are :");
for (int i = 0; i <= n; i++) {
System.out.print("\t" + FibonacciSeriesExample.fib(i));
}
}
}

Output :

Fibonacci Series are :
1 1 2 3 5 8

Java

Find factorial with recursion

Find factorial with recursion method :

recursion is a process where a method called within itself.


package com.pkjavacode.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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

int fact(int n) {
if (n == 0) {
return 1;
} else {
return n * fact(n - 1);
}
}

public static void main(String[] args) {
FactorialExample f = new FactorialExample();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
System.out.println("Enter a number :");
int b = Integer.parseInt(br.readLine());
int a = f.fact(b);
System.out.println("Factorial are :" + a);
} catch (IOException e) {
e.printStackTrace();
}
}
}

… 6 more words
Java

convert map to list in java


import java.util.*;

public class MapToList{

public static void main(String args[])
{
Map<Integer, String> map=new HashMap<Integer, String>();
map.put(1, "India");
map.put(2, "Pak");
map.put(3, "China");
map.put(4, "UsA");

List<Integer> list=new ArrayList<Integer>(map.keySet());
System.out.println("Key of map as a lis is :"+list);
List<String> list1=new ArrayList<String>(map.values());
System.out.println("value of map as a list is :"+list1);
}
}

Java