How to count number of character from Stream Method in Java 8?

To count number of character of a String using Stream API.we are using IntStream method that convert mapToInt and through forEach method traverse the data.

				
					import java.util.stream.IntStream;
import java.util.stream.Stream;

public class CountChar {
	public static void main(String[] args) {
		String[] value = { "java", "tech", "note", "com" };
		Stream<String> str = Stream.of(value);
		IntStream ins = str.mapToInt(h -> h.length());
		ins.forEach(System.out::println);
	}
}
				
			

Output :- 4 4 4 3

Leave a Comment

Your email address will not be published. Required fields are marked *