Drop Down

Monday, February 4, 2019

ReverseEachSring_StringBuffer

package interview;

public class ReverseEachSring_StringBuffer {

public static void main(String[] args)
{

String s1 = "java programming uses JVM";
String[] s2 = s1.split(" ");  //[java , programming]
StringBuffer buffer =new StringBuffer();
for(int i = 0;i<s2.length;i++)
{      
for(int j =s2[i].length()-1; j>=0 ;j--)
{

buffer = buffer.append(s2[i].charAt(j));
}
buffer = buffer.append(" ");

}
System.out.println("Original: "+s1);
System.out.println("Reversed: "+buffer);


}



}

ReverseEachString_CharArray

package interview;

public class ReverseEachString_CharArray
{
public static void main(String[] args)
{
String s1 = "java programming uses JVM";
String rev ="";
String[] s2 = s1.split(" ");
System.out.println(s1);
for(String s : s2)
{

char[] ch = s.toCharArray();

for(int i =ch.length-1 ;i>=0;i--)
{
System.out.print(ch[i]);
}

System.out.print(" ");
}

}

}

Reverse String Custom

package interview;


public class ReverseString_Custom {

public static void main(String[] args)
{
 
  String orig = "This is a Tree";
  String rev = "";
  int len = orig.length();
  for(int i = len-1;i>=0;i--)
  {
  rev = rev + orig.charAt(i);
  }
  System.out.println(orig);
  System.out.println(rev);
}

}

Reverse String

package interview;

import java.util.Scanner;

public class ReverseString {

public static void main(String[] args)
{
String original;
String rev;

System.out.println("Enter a String");
Scanner kb = new Scanner(System.in);
original = kb.nextLine();

 
  //***********Using InBuild methods
 
  StringBuffer sb = new StringBuffer(original);
  rev = sb.reverse().toString();
  System.out.println(rev);
 

}

}

Find No. of Words: No. of characters: List to duplicate Words from text File

package test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/*
No. of Words:
No. of characters:
List to duplicate Words:
D:\work\Java Work Space\files\amit.txt
*/
public class Count_Duplicate_Words_From_File
{
public static void main(String[] args) throws IOException, FileNotFoundException
{
  BufferedReader br = new BufferedReader(new FileReader("D:\\work\\Java Work Space\\files\\amit.txt"));
  StringBuffer sb = new StringBuffer();
  String s = br.readLine();
  while(s!= null)
  {
  //sb = sb.append(s); //----->White ko hatna hetu
  sb = sb.append(s.trim());
  sb = sb.append(" ");
  s = br.readLine();
  }
  br.close();
  String[] words = sb.toString().split(" ");
  wordCount(words);
  charCount(words);
  DupWords(words);
 
}

private static void DupWords(String[] words)
{
  Map <String,Integer> map = new HashMap<>();
  for(String s : words)
  {
  if(!map.containsKey(s))
  {
  map.put(s, 1);
  }
  else
  {
  map.put(s, map.get(s)+1);
  }
  }
  System.out.println("List to duplicate Words:");
  for(Map.Entry<String,Integer> entry : map.entrySet())
  {
  if(entry.getValue()>1)
  System.out.println(entry.getKey()+" : "+entry.getValue());
  }

}

private static void charCount(String[] words)
{
char[] ch;
int count = -1;
  for (String s : words)
  {
for(int i = 0;i<s.length();i++)
{
count++;
}
count++;
  }
  System.out.println("No. of characters: "+count);

}

private static void wordCount(String[] words)
{
  System.out.println(Arrays.toString(words));
  System.out.println("No of Words: "+words.length);

}
}

Sunday, February 3, 2019

CharToString

public class CharToStringExample {

       public static void main(String args[]) {
              char ch = 'U';

              // char to string using Character class
              String charToString = Character.toString(ch);
              System.out.println("Converting Char to String using Character class: " + charToString);

              // char to String using String concatenation
              String str = "" + ch;
              System.out.println("Converting Char to String using String concatenation: " + str);

              // char to String using anonymous array
              String fromChar = new String(new char[] { ch });
              System.out.println("Converting Char to String using anonymous array: " + fromChar);

              // char to String using String valueOf
              String valueOfchar = String.valueOf(ch);
              System.out.println("Converting Char to String using String valueOf: " + valueOfchar);

       }

}

Output:
Converting Char to String using Character class: U
Converting Char to String using String concatenation: U
Converting Char to String using anonymous array: U
Converting Char to String using String valueOf: U


Read more: https://javarevisited.blogspot.com/2012/02/how-to-convert-char-to-string-in-java.html#ixzz5eT208uoa

Friday, February 1, 2019

SQL Queries

--=====================Nth HIGHEST SALARY=======================
--Highest salary
select max(salary) from employees;
--2nd highest salary
select max(salary) from employees where salary < (select max(salary) from employees);
--Nth Highest Salary
SELECT EMPLOYEE_ID,FIRST_NAME, LAST_NAME,salary
FROM EMPLOYEES e1
WHERE 3 = (SELECT COUNT(DISTINCT(SALARY)) FROM
EMPLOYEES e2 WHERE e2.SALARY > e1.SALARY);
--===============================================================
--===================DELETE DUPLICATE ROWS=======================
--===============================================================
delete from emp where rowid in(
select rowid from(
select k.*,ROW_NUMBER() OVER(partition by id order by id)R ,rowid
from emp k )
where R!=1);
--Explanation
select * from emp; --13 rows
select k.* from emp k; --13 rows
select id, count(1) from emp group by id; -- to get duplicates
select id, count(1) from emp group by id having count(*)>2; --to get duplicates>2
select id, count(1) from emp group by id;
select distinct * from emp; -- 3 rows
--provide row number to it , OK
select k.*,ROW_NUMBER() OVER(order by id) from emp k; --provided row number to each rows
--Now partition table based on id
select k.*,ROW_NUMBER() OVER(partition by id order by id) from emp k; --partition of table based on id
--Here when we partition for each new ID counting starts from 1.
--select rownum, rowid
select k.*,ROW_NUMBER() OVER(partition by id order by id)R ,rowid from emp k;
---
delete from emp where rowid in(
select rowid from(
select k.*,ROW_NUMBER() OVER(partition by id order by id)R ,rowid
from emp k )
where R!=1);
--===============================================================
--===========find employees hired in last n months ==============
--===============================================================

select first_name,hire_date from employees where
to_char(hire_date,'DD')<15 and to_char(hire_date,'MM')<may;

--===============================================================
--===================*********************=======================
--===============================================================



--===============================================================
--===================*********************=======================
--===============================================================



--===============================================================
--===================*********************=======================
--===============================================================

Java 8 Notes Pics