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);
		}
	}
}
Category >> Java 8 >> Stream  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>