Thursday, 19 January 2017

Write a Java Program to remove duplicate characters from String using Collection ?

Here we will learn the program to remove all the duplicate characters from string ,suppose we have string "Malayalam" then output will be "Mym". In given string we have duplicate character like 'a', and 'l' , so the output will become "Mym".

In this program we have used LinkedHashMap to maintain character insertion order and to find the duplicate character. And we have store character as key in Map to find duplicate character.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class RemoveDuplicateCharacter {
 
 public static void removeWithCollection(String str){
  Map<Character, Integer> map = new LinkedHashMap<>();
  
  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);
   }
  }
  String output = "";
  String duplicateChar = "";
  for(Entry<Character, Integer> en : map.entrySet()){
   if(map.get(en.getKey()) == 1){
    output+=en.getKey();
   }
   else{
    duplicateChar+=en.getKey();
   }
  }
  System.out.println("Original String :\t"+str+"\nDuplicate Characters :\t"+duplicateChar+"\nOutput String :\t\t"+output);
 }
 public static void main(String[] args) {
  RemoveDuplicateCharacter.removeWithCollection("Pushkar Khosla");
  System.out.println();
  RemoveDuplicateCharacter.removeWithCollection("Malayalam");
 }
}

Output:

Original String :             Pushkar Khosla
Duplicate Characters : sha
Output String :             Pukr Kol

Original String :             Malayalam
Duplicate Characters : al
Output String :              Mym



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

What is Method References In Java ?


Method References :

Method References help us to point to methods by their names,A method references is described using ::(double colon) symbol.

It is used to point the following types of method : 
  1. static methods
  2. instance methods 
  3. constructor with new operator
Example Showing Method References:

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

public class MethodReferenceExample {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();
list.add("D");
list.add("A");
list.add("C");
list.add("B");
list.forEach(System.out::println);
}

}
Program Output :

D
A
C
B



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Wednesday, 18 January 2017

Write a Java Program to Count UpperCase Character in Given String ?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class CountUpperCaseCharacter {
 
 public static void getCount(String s1){
  int count = 0;
  for(char ch : s1.toCharArray()){
   if(Character.isUpperCase(ch)){
    count++;
   }
  }
  
  System.out.println("TOTAL UPPERCASE CHARACTER IN STRING : "+s1+" ,is : "+count);
 }
 public static void main(String[] args) {
  CountUpperCaseCharacter.getCount("Pushkar");
  CountUpperCaseCharacter.getCount("AbcDffffGH");
 }
}

Output:

TOTAL UPPERCASE CHARACTER IN STRING : Pushkar ,is : 1
TOTAL UPPERCASE CHARACTER IN STRING : AbcDffffGH ,is : 4


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Tuesday, 17 January 2017

Write a java program to count the number of words in a String ?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class CountWords {

 public static void getCount(String str){
  System.out.println("INPUT STRING : "+str);
  String[] words = str.split(" ");
  System.out.println("TOTAL WORDS IN STRING : "+words.length);
 }
 public static void main(String[] args) {
  CountWords.getCount("String is Immutable in Java");
 }

}

Output:

INPUT STRING : String is Immutable in Java
TOTAL WORDS IN STRING : 5


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Monday, 16 January 2017

Write a Java Program to Sort the String ?


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Arrays;

public class StringSorting {

// Method 1 WithOut String API
 public static void withoutStringAPI(String str){
  char[] arr = str.toCharArray();
  
  for(int i=0;i<str.length();i++){
        for(int j=0;j<str.length();j++){
             if(arr[i] <= arr[j]){
                   char temp = arr[i];
                   arr[i] = arr[j];
                   arr[j] = temp;
             }
        }
  }
  System.out.println("METHOD 1 OUTPUT : ");
  for(char c : arr){
        System.out.print(c);
  }
 }

// Method 1 With String API
 public static void withStringAPI(String str){
  char[] arr = str.toCharArray();
  Arrays.sort(arr);
  System.out.println("\n\nMETHOD 2 OUTPUT : ");
  for(char c : arr){
         System.out.print(c);
  }
 }
 public static void main(String[] args) {
         StringSorting.withoutStringAPI("pushkar");
         StringSorting.withStringAPI("bcda");
 }
}

Output :

METHOD 1 OUTPUT : 
ahkprsu

METHOD 2 OUTPUT : 
abcd


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Sunday, 15 January 2017

Explain Exception hierarchy in Java ?

Exception :

The Java Exception are of three types i.e. Checked Exception ,Unchecked Exception and Errors ,checked exception are also known as CompileTimeException and unchecked exception are also known as RunTimeException

Exception is a mechanism which must be handle so that normal flow of application can take place, because exception is an abnormal condition which distrupts the normal flow of application.

Checked Exceptions are those exception which are caught at compile time. Some Exceeption are such as IOException, SQLException, CloneNotSupportedException and many more.

Unchecked Exceptions are those exceptions which are caught at run time. Some Exception are such as NullPointerException, ArithmeticException, ArrayIndexOutOfBoundException ,NumberFormatException and many more.

Errors is a subclass of Throwable Exception that indicates serious problem and that can't be catch. Errors are abnormal conditions. Some Errors are such as NoClassDefFoundError, InternalError, StackOverflowError and many more.

Exception Hierarchy :





      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Saturday, 14 January 2017

What are rules of Method Overriding in Java ?

Some Rules of Method Overriding are as follows :
  1. Methods must have same name.
  2. Methods must have same number of argument list.
  3. Method must have same return type.
  4. Only public, protected and default(same package) methods can be overridden.
  5. If method has default access, then overriding method must be default, protected or public.
  6. If method has protected access, then overriding method must be protected or public.
  7. If method has public access, then overriding method must be public.
  8. Final methods cannot be overridden. Because final is used to declare constant.
  9. Static methods cannot be overridden. Because static method is available to all instance of super class and sub class.So it's not permissible to re-implement the static method in sub class.
  10. Use super keyword to invoke the parent class method from sub class.
  11. Constructor can not overridden.
  12. The synchronized modifier has no effect on the rules of overriding.
  13. The strictfp modifier has no effect on the rules of overriding.




      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Read More »

What is JSP ?

JSP(Java Server Page) :

JSP was released in 1999 by Sun Microsystems. JSP is just like PHP and ASP but JSP uses Java Programming Language. 

JSP stands for Java Server Page. Jsp is an extension of servlet technology . JSP provide more functionality than servlet such as Expression Language, JSTL(JSP Standard Tag Library) etc.

Jsp page consists of HTML and JSP tags. Jsp is an technology that is used for developing web pages that supports dynamic content. 

JSP is easy to maintain than servlet because we can separate design and development logic. JSP pages don't need to recompile or redeploy but servlet code always recompiled if any modification take place in servlet.

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Thursday, 12 January 2017

What are different types of comments in JSP ?

Comments tags are used to insert comments in the source code. These comment tags are used to place some notification or remainders in the source code. Comments are not displayed on the browser, they only help in document your source code.

Comments are also helpful in debugging the code, because we can comment out the lines of code to search for errors.

There are three types of comments in JSP :

1) HTML comments : 
Html comment syntax are also used in XML and JSP. We can use HTML comments in the JSP page by using the following syntax:
<!-- This is a comment -->
One thing notice that there is an exclamation (!) point in opening tag,but not in closing tag.

2) JSP Comments :
Jsp Comments are also known as Hidden Comments, because these comments are invisible when jsp page is translated into servlet internally. Syntax of using JSP comments are :
<%--This is a comment  %>

3) Java Comments :
Java Comments are of two types i.e. first is single line comment and other is multiline comment. Java comments are only allowed in JSP scriptlet tag because java code can only insert into scriptlet tags.

Syntax of using single line comment :

// Single line comment

Syntax of using multiline line comment :

/*
Multiline comment
*/

      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Read More »

Wednesday, 11 January 2017

What is load-on-startup In Servlet ?


Load-on-startup:

Servlet Container loads the servlet on the first request, but if the servlet is heavy, then it takes much time to loads. Then we can load those servlet on application starts. 

So we can load heavy servlet on application startup in two ways i.e. first way is to use <load-on-startup> tag under the <servlet> configuration tag in web.xml, and second way is to use @WebServlet(loadOnStartup=1) annotation over the servlet name for annotation based configuration.

In load-on-startup configuration we provide integer values ,if it is negative(-) value then servlet container will load the servlet on request, if it is positive values i.e. from 0 then servlet container will load servlet on application startup.

If multiple servlet configuration contains load-on-startup configuration and having values such as 0,1,2,3,4 and so on then servlet container will starts loading servlet from lower integer values and so on.

Web.xml Configuration Example:

1
2
3
4
5
<servlet>
     <servlet-name>HomeSevlet</servlet-name>
     <servlet-class>org.servlet.HomeSevlet</servlet-class>
     <load-on-startup>1</load-on-startup>
</servlet>

Annotation Configuration Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@WebServlet(value="/HomeSevlet", loadOnStartup=1)
public class InitializeResources extends HttpServlet {

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      System.out.println("HEREEEE");
  }

}


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

Tuesday, 10 January 2017

What is difference between forward() and sendRedirect() ?

Here we will list all the differences between forward() and sendRedirect().

Forward
SendRedirect
It’s a method of RequestDispatcher.
It’s a method of HttpServletResponse.
This method is internally handled by container.
This method is not internally handling by container.
This method doesn’t change the browser URL.
This method changes the browser URL.
Forward sends the same request.
SendRedirect always sends the new request.
It can only send the request with in same application, or it can only access those resources which are available in same application.
It can send the request with in same application and outside the application, and can access inside and outside resources of server.
It only uses Relative URLs.
It uses Absolute URLs.
It’s a server-side.
It’s a client-side.
Method Signature :
void forward(ServletRequest request, ServletResponse response);
Method Signature :
void sendRedirect(String url);
Example :
request.getRequestDispathcer("login.jsp").
forward(request, response);
Example :
response.sendRedirect("login.jsp")


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »

What is difference between PrintWriter and ServletOutputStream ?

Here we will list all the differences between PrintWriter and ServletOutputStream:

PrintWriter
ServletOutputStream
It’s a character stream (useful for text data).
It’s a byte stream (useful for graphical data).
It provide getWriter() method.
It provide getOutputStream() method.
It does character encoding for ASCII / Unicode characters.
It does not perform any character encoding because it if meant for binary data.
It’s a Concrete class.
It’s Abstract class.
Its performance is little slow because of character conversion.
Its performance is fast because no data conversion takes place.
Example :
PrintWriter out = response.getWriter();
out.println("Welcome User");                    
Example :
ServletOutputStream out = resonse.getOutputStream();
out.write(barray);


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Read More »