find length of a string array using Java 8 streams.

To find length of a string array using Java 8 streams, we are doing following step.

  • First take a String input array.
  • Stream.of(str) used to create a sequential Stream from several arguments.
  • Here r.length() method give the string length.
  • Now mapToInt transform a stream of objects into an IntStream of primitive int values.
  • Print each string of length through forEach method.
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class LengthOfString {
	public static void main(String[] args) {
		String[] str = { "java", "spring", "hibernate", "com", "javatechnote" };
		System.out.println("length of all string are :- ");
		IntStream in = Stream.of(str).mapToInt(r -> r.length());
		in.forEach(System.out::println);
	}
}

Output :-
length of all string are :-
4
6
9
3
12