Saturday, 17 September 2016

Java Program to Traverse List of Class Type using Lambda Expression?

import java.util.ArrayList;
import java.util.List;

class Record{
private String code;
private String desc;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
public class TraverseListOfClassTypeUsingLambdaExpression {

public static void main(String[] args) {
List<Record> list = new ArrayList<>();
                for(int i=0;i<5;i++){
             Record r = new Record();
             r.setCode("CODE : "+i);
             r.setDesc("DESC : "+i);
             list.add(r);
                }
        
             /*Iterator<Record> it = list.iterator();
              Record r1 = null;
              while(it.hasNext()){
        r1 = it.next();
        System.out.println(r1.getCode()+"\t"+r1.getDesc());
              }*/
        
              //Lambda Expression to print ArrayList of class type
              list.forEach(l->System.out.println(l.getCode()+"\t"+l.getDesc()));
}

}

Program Output :-

CODE : 0 DESC : 0
CODE : 1 DESC : 1
CODE : 2 DESC : 2
CODE : 3 DESC : 3

CODE : 4 DESC : 4


Java Program to Traverse Map using Lambda Expression?

import java.util.HashMap;
import java.util.Map;

public class TraversingMap{
         public static void main(String[] args){
                  Map<Integer, Integer> map = new HashMap<>();
                  map.put(0, 100);
                  map.put(1, 101);
                  map.put(2, 103);                  
                  map.put(3, 104);
                  map.put(4, 105);
                  map.put(5, 106);
                  
                  System.out.println("Traversing Map : ");                   map.forEach((k,v)-> System.out.println(k+"\t"+v));

                  System.out.println("Traversing Map With condition : ");
                  map.forEach((k,v)-> {
                 if(k == 2){
               System.out.println(k+"\t"+v);
                 }
                  });                  
         }

}

Program Output :-

Traversing Map : 
0 100
1 101
2 103
3 104
4 105
5 106

Traversing Map With condition : 
2 103



Wednesday, 14 September 2016

Java Program to find Unique elements From Array?

public class DistinctElementsFromArray {

public static void getElements(int[] arr){
System.out.println("UNIQUE ELEMENTS FROM ARRAY : ");
for(int i=0;i<arr.length;i++){
boolean unique = false;
for(int j=0;j<i;j++){
if(arr[i] == arr[j]){
unique = true;
}
}
if(!unique){
System.out.print(arr[i]+",");
}
}
}
public static void main(String[] args) {
int[] arr = {5,2,5,3,2,8,7};
DistinctElementsFromArray.getElements(arr);
}

}

Program Output :-

UNIQUE ELEMENTS FROM ARRAY : 
5,2,3,8,7,




Tuesday, 13 September 2016

Java program to Convert Binary To Decimal?

The decimal number is equal to the sum of powers of 2 of the binary number's '1' digits place . Here we add only those powers whose binary is one. For Example :-
Binary Number
1
1
1
0
0
0
0
Power of 2
26
25
24
23
22
21
20
Value
64
32
16
8
4
2
 1

                       64  +   32   +   16   +    0   +   0   +   0   +   0   = 112


public class BinaryToDecimal {

public static void getDecimal(int num){
int binary = num;
int i = 0;
int decimal = 0;
while(num > 0){
int temp = num % 10;
decimal+=Math.pow(2, i++)*temp; 
num = num / 10;
}
System.out.println("DECIMAL OF "+binary+" : "+decimal);
}
public static void main(String[] args) {
BinaryToDecimal.getDecimal(11);
BinaryToDecimal.getDecimal(1110000);
}

}

Program Output :-

DECIMAL OF 11 : 3
DECIMAL OF 1110000 : 112


Java Program to Convert Decimal to Binary?

Below example shows how to convert Decimal number into binary ,as we Know that for converting decimal to binary we divide the number by 2 untill the quotient become zero or one ,and then we reverse the remainder .

public class DecimalToBinary {

public static void getBinary(int num){
int local = num;
int[] arr = new int[num];
int place=0;

while(num > 0){
int temp = num % 2;
arr[place++] = temp;
num = num / 2;
}
System.out.print("BINARY OF "+local+" : ");
for(int j = place-1 ;j >= 0 ;j--){
System.out.print(arr[j]);
}

}
public static void main(String[] args) {
DecimalToBinary.getBinary(112);
}

}

Program Output :-

BINARY OF 112 : 1110000



Java Program for Bubble Sort?

Bubble sort algorithm is the simplest sorting algorithm. In bubble sort algorithm, array is traversed from first element to last element.  Here, first element is compared with the next element. If first element is greater than the next element,then it is swapped.

import java.util.Arrays;

public class BubbleSort {

public static void method_1(int[] arr){
System.out.println("ELEMENTS BEFORE SORTING : ");
for (int i : arr) {
System.out.print(i+",");
}

for(int i=0 ; i < arr.length ; i++){
for(int j=0 ; j < arr.length-1 ;j++){
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

System.out.println("\n\nELEMENTS AFTER SORTING : ");
for (int i : arr) {
System.out.print(i+",");
}
}
public static void method_2(int[] arr){
System.out.println("\n\nSORTING USING PREDEFINED METHOD : ");
Arrays.sort(arr);
for(int i : arr){
System.out.print(i+",");
}
}
public static void main(String[] args) {
int[] arr = {5,6,3,1,2};
BubbleSort.method_1(arr);
BubbleSort.method_2(arr);
}

}

Program Output :-

ELEMENTS BEFORE SORTING : 
5,6,3,1,2,

ELEMENTS AFTER SORTING : 
1,2,3,5,6,

SORTING USING PREDEFINED METHOD : 

1,2,3,5,6,


Friday, 9 September 2016

Java Program to find Prime Numbers From 1 to N?

Below Example shows how to find all Prime Numbers from 1 to N.

public class PrimeNumbersFromOneToN {

public void allPrimeNumbers(int num){
System.out.println("Prime Numbers From 1 to "+num);
for(int i=1;i<num;i++){
boolean isPrime = true;
for(int j=2;j<i;j++){
if(i % j == 0){
isPrime = false;
break;
}
}
if(isPrime){
System.out.print(i+",");
}

}
}
public static void main(String[] args) {
PrimeNumbersFromOneToN p = new PrimeNumbersFromOneToN();
p.allPrimeNumbers(100);
}

}

Program Output :-

Prime Numbers From 1 to 100
1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,



Java Program to check Number is Prime or Not?

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.


public class NumberIsPrimeOrNot {

public static boolean isPrime(int num){
for(int i=2;i<=num/2;i++){
if(num % i == 0){
return false;
}
}
return true;
}
public static void main(String[] args) {
System.out.println("IS 17 PRIME NUMBER : "+NumberIsPrimeOrNot.isPrime(17));
System.out.println("IS 5 PRIME NUMBER : "+NumberIsPrimeOrNot.isPrime(5));
}

}

Program Output :-

IS 17 PRIME NUMBER : true
IS 5 PRIME NUMBER : true



Thursday, 8 September 2016

Java Program to Sum each Digit of Given Number?

Below Example shows how to Sum each Digit of Number. The Logic behind the sum of each digits is that divide the number by 10 and add Reminder to separate variable, Repeat this process until number become 0.


public class SumEachDigitOfNumber {
public static int getSum(int num){
int sum = 0;
if(num == 0){
return num;
}
else{
while(num > 0){
int temp = num%10;
sum+=temp;
num = num/10;
}
}
return sum;
}
public static void main(String[] args) {
System.out.println("SUM OF DIGITS : "+SumEachDigitOfNumber.getSum(222));
}


}

Program Output :-

SUM OF DIGITS : 6







Java Program to Swap Two Variables Without Using Third Variable?

Below Example shows how to swap two variables without using third variable. The Simple logic behind swapping is to add and subtract both values in given variables.

public class SwapTwoNumbersWithoutThirdVariable {

public static void main(String[] args) {
int a = 12;
int b = 6;

System.out.println("BEFORE SWAP : A = "+a+" ,B = "+b);
a = a+b;
b = a-b;
a = a-b;
System.out.println("AFTER  SWAP : A = "+a+" ,B = "+b);
}

}

Program Output :-

BEFORE SWAP : A = 12 ,B = 6

AFTER  SWAP : A = 6 ,B = 12



Tuesday, 6 September 2016

Java Program to find Common Elements from two Arrays ?

Below Example shows how to find common elements from two input arrays.

public class CommonElementsFromTwoArray {

public static void main(String[] args) {
int[] input1 = {4,5,9,8,2,6,3};
int[] input2 = {4,2,6,0,1};

System.out.println("Common Elements In Both Arrays :-");
for(int i=0; i<input1.length; i++){
for(int j=0; j<input2.length; j++){
if(input1[i] == input2[j]){
System.out.println(input1[i]);
}
}
}
}
}

Program Output :-

Common Elements In Both Arrays :-
4
2
6



Java Program to check Number is Perfect or Not ?

A perfect number is a positive integer that is equal to the sum of its positive divisors, that is, the sum of its positive divisors excluding the number itself.
For Example The first perfect number is 6, because 1, 2, and 3 are its positive divisors, and sum of  1 + 2 + 3 = 6. Other Perfect Numbers are 28 ,496 and so on.


public class PerfectNumber {

public static boolean isNumPerfect(int num){
boolean status = false;
int temp = 0;
for(int i=1;i<=num/2;i++){
if(num%i == 0){
temp+= i;
}
}
if(temp == num){
status = true;
} else{
status = false;
}
return status;
}
public static void main(String[] args) {
System.out.println("Is 6 Perfect Number : "+PerfectNumber.isNumPerfect(6));
System.out.println("Is 28 Perfect Number : "+PerfectNumber.isNumPerfect(28));
System.out.println("Is 30 Perfect Number : "+PerfectNumber.isNumPerfect(30));
}
}

Program Output :-

Is 6 Perfect Number : true
Is 28 Perfect Number : true

Is 30 Perfect Number : false



Java Code to Avoid Dead Lock ?

There would not be any deadlock because both methods are accessing lock on Integer and String class literal in same order. So, if thread A acquires lock on Integer object , thread B will not proceed until thread A releases Integer lock, same way thread A will not be  blocked even if thread B holds String lock because now thread B will not expect thread A to release Integer lock to proceed further.

public class DeadLockAvoid {
public void method1(){
synchronized(String.class){
System.out.println("Aquired lock on String.class object");
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");
}
}
}
public void method2(){
synchronized(String.class){
System.out.println("Aquired lock on String.class object");
synchronized(Integer.class){
System.out.println("Aquired lock on Integer.class object");
}
}
}
}


Saturday, 3 September 2016

JavaScript And Jquery Function.

JavaScript And Jquery Function are comming Soon ...

Java Program to find First And Second Maximum Number From int type Array ?

Below example show how to find first and second largest Number From int type Array.

public class TwoMaximumNumberFromList {

public static void method(int[] num){
int firstMax = 0;
int secondMax = 0;
for(int n : num){
if(n > firstMax){
secondMax = firstMax;
firstMax = n;
}
else if(secondMax < n){
secondMax = n;
}
}
System.out.println("FIRST MAXIMUM NUMBER : "+firstMax);
System.out.println("SECOND MAXIMUM NUMBER : "+secondMax);
}
public static void main(String[] args) {
int[] num={5,6,9,3};
TwoMaximumNumberFromList.method(num);
}

}

Program Output :-
FIRST MAXIMUM NUMBER : 9

SECOND MAXIMUM NUMBER : 6




Java Program to find Duplicate Character In String ?

Below example show how to count repeated characters from String.

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class DuplicateCharacterFromString {

public static void method(String str){
Map<Character, Integer> map = new HashMap<>();
for(int i=0;i<str.length();i++){
if(map.containsKey(str.charAt(i))){
map.put(str.charAt(i), map.get(str.charAt(i))+1);
}
else{
map.put(str.charAt(i), 1);
}
}
Set<Character> set = map.keySet();
for(Character c : set){
if(map.get(c) > 1){
System.out.println(c+":"+map.get(c));
}
}
}
public static void main(String[] args) {
DuplicateCharacterFromString.method("abca");
}


}
Program Output :-

a:2