Kubernate

Java8 # program - Print only Specific charachter

 Here's a simple Java 8 program that prints the character 'w' from the string "swiss" using a stream-based approach:

import java.util.stream.IntStream;

public class PrintWFromString {
    public static void main(String[] args) {
        String s = "swiss";

        // Using Java 8 streams to filter and print 'w'
        IntStream.range(0, s.length())
                 .mapToObj(s::charAt)
                 .filter(ch -> ch == 'w')
                 .forEach(System.out::println);
    }
}

๐Ÿ” Explanation

  • IntStream.range(0, s.length()): Creates a stream of indices from 0 to the length of the string.
  • .mapToObj(s::charAt): Converts each index to the corresponding character.
  • .filter(ch -> ch == 'w'): Filters only the 'w' characters.
  • .forEach(System.out::println): Prints each matching character.

This will output:

w


No comments:

Post a Comment

Spring Boot - Bean LifeCycle

 Here is a clear, step-by-step lifecycle of a Spring Boot application , explained in a simple + interview-ready way. ๐Ÿ”„ Spring Boot Applica...

Kubernate