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