write a ‘java’ program to display characters from ‘a’ to ‘z’

 Here is a simple program that prints characters from a to Z

  1. public class DisplayAlphabets {
  2.     public static void main(String[] args) {
  3.         for (char c = 'a'; c <= 'z'; c++) {
  4.             System.out.print(c + " ");
  5.         }
  6.     }
  7. }

This program uses a for loop to iterate through the characters from 'a' to 'z'. The loop variable "c" is initialized to 'a' and incremented by 1 on each iteration until it reaches 'z'. Inside the loop, the current character is printed using the System.out.print() method. The " " at the end of the print statement will print the character followed by a space.

Alternatively, we can use the Character.toChars(int) method to convert the int value to a char and use the for loop to iterate from 97 to 122.

java program to print characters from a to z


This will also display the same output as the first example: "a b c d e f g h i j k l m n o p q r s t u v w x y z".
Note that, in both examples, the alphabets will be printed in lowercase. If we want to print uppercase alphabets, you can use 'A' to 'Z' in the for loop or use 65 to 90 in the Character.toChars(int) method.

We can also use the java.util.stream.IntStream to display characters from 'a' to 'z'.
 Here is an example of how you can use IntStream to display characters from 'a' to 'z':

public class DisplayAlphabets {
    public static void main(String[] args) {
        IntStream.rangeClosed('a', 'z').mapToObj(c -> (char) c).forEach(System.out::print);
    }
}

This program uses the IntStream.rangeClosed() method to create a stream of ints from the ASCII value of 'a' to the ASCII value of 'z'. Then it uses the mapToObj() method to convert the ints to chars, and finally the forEach() method to print each character.

we can also use the java.util.stream.IntStream to display characters from 'A' to 'Z' using the same approach. Here is an example of how you can use IntStream to display characters from 'A' to 'Z':


public class DisplayAlphabets {
    public static void main(String[] args) {
        IntStream.rangeClosed('A', 'Z').mapToObj(c -> (char) c).forEach(System.out::print);
    }
}

This will print the uppercase alphabets : "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z".

It's worth noting that, in all examples the alphabets will be displayed in a single line, if you want to display them in separate lines, you can modify the System.out.print() method to System.out.println().

How to Convert string to StringBuilder and vise versa in Java


  • We can Covert String to string builder in two ways
  1. Using constructor of StringBuilder(String s)
  2. Using append method of StringBuilder 
  • Lets see a java program to convert java String object to StringBuilder using constructor of StringBuilder class.

#1: Java program to convert string to StringBuilder using constructor of StringBuilder class.



  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programs
  5.  * 
  6.  * Description: Java Program to convert String to StringBuilder
  7.  *
  8.  */
  9. public class StringBuilderDemo {

  10. public static void main(String[] args) {
  11. String str="java";
  12. StringBuilder sb = new StringBuilder(str);
  13. String s = sb.toString();
  14. System.out.println(s);
  15. StringBuilder stebldr= new StringBuilder("convert string to stringbuilder");
  16. String st = stebldr.toString();
  17. System.out.println(st);
  18. }

  19. }

Output

  1. java
  2. convert string to stringbuilder

#2: Java program to convert string to StringBuilder using append method of StringBuilder class.
  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programs
  5.  * 
  6.  * Description: Java Program to convert String to StringBuilder
  7.  *
  8.  */
  9. public class StringBuilderDemo {

  10. public static void main(String[] args) {

  11. StringBuilder sb = new StringBuilder();
  12. sb.append("Java convert string to StringBuilder ");
  13. System.out.println(sb);
  14. String str= "Convert string to Stringbuilder" ;
  15. sb.append(str);
  16. System.out.println(sb);
  17. }

  18. }


Output

  1. Java convert string to StringBuilder 
  2. Java convert string to StringBuilder Convert string to Stringbuilder

  • We can Convert StringBuilder to String by using toString() method of StringBuilder class.
  • How to convert StringBuilder to String in java.


#3: Java program to convert string to StringBuilder using append method of StringBuilder class.

  1. package com.instanceofjava;
  2. /**
  3.  * @author www.Instanceofjava.com
  4.  * @category interview programs
  5.  * 
  6.  * Description: Java Program to convert StringBuilder to String
  7.  *
  8.  */
  9. public class StringBuilderDemo {

  10. public static void main(String[] args) {

  11. StringBuilder sb = new StringBuilder();
  12. sb.append("Java convert  StringBuilder to string");
  13. String str=sb.toString();
  14. System.out.println(str);
  15. String s= "Convert Stringbuilder to string " ;
  16. sb.append(s);
  17. String st=sb.toString();
  18. System.out.println(st);
  19. }

  20. }

Output

  1. Java convert  StringBuilder to string
  2. Java convert  StringBuilder to stringConvert Stringbuilder to string 


java program to convert string to string builder

Java Program to check a String is palindrome or not by ignoring its case


  • A palindrome is a word that reads the same backward or forward.
  • Lets see what if the characters in a palindrome string are in different case. i.e should not be case sensitive. 
  • So now we will see how to check a string is palindrome or not by ignoring its case.
  • Write a function which checks  that string is palindrome or not by converting the original string to lowercase or uppercase.
  • Method should return true if it is palindrome, return false if not a palindrome.


#1 : Java Program to check given string is palindrome or not by ignoring its case.

  1. package com.instanceofjava;

  2. public class CheckPalindrome {
  3. public static boolean isPalindrome(String str) {
  4. StringBuffer strone=new StringBuffer(str.toLowerCase());
  5. StringBuffer strtwo=new StringBuffer(strone);
  6.  
  7.   strone.reverse();
  8.  
  9.   System.out.println("Orginal String ="+strtwo);
  10.   System.out.println("After Reverse ="+strone);
  11.  
  12. if(String.valueOf(strone).compareTo(String.valueOf(strtwo))==0)
  13. return true;
  14.     else
  15.     return false;
  16. }
  17. public static void main(String[] args) {
  18. boolean ispalindrome= isPalindrome("DeleveleD");
  19. System.out.println(ispalindrome);
  20.  
  21.     }

  22. }

Output

  1. Orginal String =deleveled
  2. After Reverse =deleveled
  3. true

check palindrome or not ignore case sensitive

How to Delete folder and subfolders using Java 8

  • How to delete folder with all files and sub folders in it using java 8.
  • We can use java 8 Stream to delete folder recursively 
  • Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)
  • .sorted(Comparator.reverseOrder())
  • .map(Path::toFile)
  • .peek(System.out::println)
  • .forEach(File::delete);

  1. Files.walk -  this method return all files/directories below the parent folder
  2. .sorted - sort the list in reverse order, so the folder itself comes after the including subfolders and files
  3. .map - map the file path to file
  4. .peek - points to processed entry
  5. .forEach - on every File object calls the .delete() method 




#1: Java Example program to delete folders and subfolders using java 8 stream

  1. package com.instanceofjava;

  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.file.FileVisitOption;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.Paths;
  8. import java.util.Comparator;
  9. /**
  10.  * @author www.Instanceofjava.com
  11.  * @category interview questions
  12.  * 
  13.  * Description: delete folders and sub folders using java 8
  14.  *
  15.  */

  16. public class DeleteFolder {

  17. public static void main(String[] args) {
  18. Path rootPath = Paths.get("C:\\Users\\Saidesh kilaru\\Desktop\\folder1");
  19. try {
  20. Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)
  21.     .sorted(Comparator.reverseOrder())
  22.     .map(Path::toFile)
  23.     .peek(System.out::println)
  24.     .forEach(File::delete);
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }

  29. }

Output:

  1. C:\Users\Saidesh kilaru\Desktop\folder1\subfolder\file2 in sub folder.docx
  2. C:\Users\Saidesh kilaru\Desktop\folder1\subfolder
  3. C:\Users\Saidesh kilaru\Desktop\folder1\file1.docx
  4. C:\Users\Saidesh kilaru\Desktop\folder1



How to Convert integer set to int array using Java 8

  • How to convert Integer Set to primitive int array.
  • By Using java 8 Streams we can convert Set to array.
  • set.stream().mapToInt(Number::intValue).toArray();
  • Lets see an example java program on how to convert integer set to int array using java 8

#1: Java Example program on converting Integer Set to int Array

  1. package com.instanceofjava;

  2. import java.util.Arrays;
  3. import java.util.HashSet;
  4. import java.util.Set;

  5. public class SetToArray {
  6. /**
  7.  * @author www.Instanceofjava.com
  8.  * @category interview programming questions
  9.  * 
  10.  * Description: convert Integer set to int array using java 8
  11.  *
  12.  */
  13. public static void main(String[] args) {
  14. Set<Integer> hashset= new HashSet<>(Arrays.asList(12,34,56,78,99));
  15. int[] array = hashset.stream().mapToInt(Number::intValue).toArray();
  16. for (int i : array) {
  17. System.out.println(i);
  18. }

  19. }

  20. }

Output:

  1. 34
  2. 99
  3. 56
  4. 12
  5. 78





integer set to integer array java


In Java, a Set is a collection that contains no duplicate elements and is unordered. To convert a Set to an array, you can use the toArray() method of the Set interface. This method returns an array containing all of the elements in the Set in the order they are returned by the iterator.

Set<Integer> set = new HashSet<>();
set.add(1);
set.add(2);
set.add(3);

Integer[] array = set.toArray(new Integer[set.size()]);



In this example, we first create a HashSet of integers, add some elements to it, then we use the toArray method to convert it to an array of integers.

toArray(T[] a) method where we can pass an empty array of a specific type, the method will fill the array with the elements from the set, this is useful if you know the size of the array that you need.

converting a Set to an array in Java can be done using the toArray() method of the Set interface. The method returns an array containing all of the elements in the Set in the order they are returned by the iterator. This method can also accept an empty array of a specific type, the method will fill the array with the elements from the set.

Initializing a boolean array in java with an example program

  • Initializing a boolean variable : boolean b=true;
  • In some cases we need to initialize all values of boolean array with true or false.
  • In such cases we can use Arrays.fill() method
  • Arrays.fill(array, Boolean.FALSE);
  • java initialize boolean array with true:  Arrays.fill(array, Boolean.FALSE);
  • Lets see an example java program on how to assign or initialize boolean array with false or true values.



#1: Java Example program on initializing boolean array.

  1. package com.instanceofjava;

  2. import java.util.Arrays;
  3. /**
  4.  * @author www.Instanceofjava.com
  5.  * @category interview questions
  6.  * 
  7.  * Description: Initialize boolean array values with false or true
  8.  *
  9.  */
  10. public class InitializeBoolean {

  11. public static void main(String[] args) {
  12. Boolean[] array = new Boolean[4];
  13. //initially all values will be null
  14. for (int i = 0; i < array.length; i++) {
  15. System.out.println(array[i]);
  16. }
  17. Arrays.fill(array, Boolean.FALSE);
  18. // all values will be false
  19. for (int i = 0; i < array.length; i++) {
  20. System.out.println(array[i]);
  21. }
  22. Arrays.fill(array, Boolean.TRUE);
  23. // all values will be false
  24. for (int i = 0; i < array.length; i++) {
  25. System.out.println(array[i]);
  26. }
  27. }

  28. }

Output:

  1. null
  2. null
  3. null
  4. null
  5. false
  6. false
  7. false
  8. false
  9. true
  10. true
  11. true
  12. true


java initialize boolean array with true

Java 8 initialize set with values with an example program

  • We can initialize set while defining by passing values to constructor.
  • For example to initialize HashSet we can use Arrays.asList(value1,value2).
  • Set<Integer> hashset = new HashSet<>(Arrays.asList(12, 13));

#1: Java Example program to initialize set without using java 8

  1. import java.util.Arrays;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4. /**
  5.  * @author www.Instanceofjava.com
  6.  * @category interview questions
  7.  * 
  8.  * Description: Initialize set 
  9.  *
  10.  */
  11. public class InitializeSet {

  12. public static void main(String[] args) {

  13. Set<Integer> hashset = new HashSet<>(Arrays.asList(12, 13));
  14. System.out.println(hashset);
  15. }

  16. }

Output:

  1. [12, 13]


  • We can initialize set in java 8 using Stream.
  • Stream.of("initialize", "set").collect(Collectors.toSet());



#2: Java Example program to initialize set without using java 8

  1. import java.util.Set;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.Stream;
  4. /**
  5.  * @author www.Instanceofjava.com
  6.  * @category interview questions
  7.  * 
  8.  * Description: Initialize set using java 8 Stream
  9.  *
  10.  */
  11. public class InitializeSet {

  12. public static void main(String[] args) {

  13. Set<String> set = Stream.of("initialize", "set").collect(Collectors.toSet());
  14. System.out.println(set);
  15. }

  16. }



Output:

  1. [set, initialize]


  • We can initialize Set in java 8 by creating stream from an Array and list


#3: Java Example program to initialize set without using java 8


java 8 initialize set with values with an example program
Select Menu