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 »