Transform Stream using Steam.map()

The map() method is an intermediate operation. It returns a stream consisting of the results of applying the given function to each element in the stream.

The following code returns a stream of Integers, which are results of applying String.length() method.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
 
 
public class Java8Map {
 
	public static void main(String[] args) {
		List<String> list = new ArrayList<String>();
		list.add("java");
		list.add("php");
		list.add("python");
 
		//map function
		Stream<Integer> stream = list.stream().map(p -> p.length());
 
		Integer[] lengthArr = stream.toArray(Integer[]:: new);
 
		for(int a: lengthArr){
			System.out.println(a);
		}
	}
}

1 thought on “Transform Stream using Steam.map()”

Leave a Comment