1. Overview
In this article, we'll be learning about a new map() function in Java 8 that is introduced in Stream API. map() method converts a Stream from one form to another Stream. This takes input X type Stream and converts into Y type output Stream.
This is widely used as part of new JDK 8 api.
2. Java 8 Stream map() Syntax
Below is the standard syntax from java api.
<R> Stream<R> map(Function<? super T,? extends R> mapper)
map() returns a stream consisting of the results of applying the given function to the elements of this stream.
This method takes the Function function interface as an argument and returns a new Stream. This is also an intermediate operation. map() operation is a lazy one and will be executed after invoking the terminal operation such as forEach(), min(), max() and collect() methods.
map() method logic is map(value -> value * 5); here. This will be executed after invocation of forEach() method in the next line.
Output:
Output:
This method takes the Function function interface as an argument and returns a new Stream. This is also an intermediate operation. map() operation is a lazy one and will be executed after invoking the terminal operation such as forEach(), min(), max() and collect() methods.
3. Java Stream map() example to find the multiplication of 5
map() method logic is map(value -> value * 5); here. This will be executed after invocation of forEach() method in the next line.
package com.java.w3schools.blog.java.program.to.java8.map; import java.util.stream.Stream; /** * Java program to find the multiplications of 5 using stream map() function. * * @author JavaProgramTo.com * */ public class StreamMapExample1 { public static void main(String[] args) { // Creating int stream using Stream.of() method. Stream<Integer> intValues = Stream.of(1, 2, 3, 4, 5, 6, 7); // multiplication of 5 Stream fifthTableStream = intValues.map(value -> value * 5); // iterating final stream fifthTableStream.forEach(output -> System.out.println(output)); } }
Output:
5 10 15 20 25 30 35
4. Java Stream map() example to find the length of each string
package com.java.w3schools.blog.java.program.to.java8.map; import java.util.stream.Stream; /** * Java program to find each string length using stream map() function. * * @author JavaProgramTo.com * */ public class StreamMapExample2 { public static void main(String[] args) { // Creating string stream using Stream.of() method. Stream<String> intValues = Stream.of("java", "program", "to", "com", "java-w3schools", "blog", ""); // string length stream Stream<Integer> lengthStream = intValues.map(string -> string.length()); // iterating final stream lengthStream.forEach(output -> System.out.println(output)); } }
Output:
4 7 2 3 14 4 0
5. Java Stream map() example to convert to UpperCase
package com.java.w3schools.blog.java.program.to.java8.map; import java.util.stream.Stream; /** * Java program to convert into uppercase using stream map() function. * * @author JavaProgramTo.com * */ public class StreamMapExample3 { public static void main(String[] args) { // Creating string stream using Stream.of() method. Stream<String> intValues = Stream.of("java", "program", "to", "com", "java-w3schools", "blog", ""); // upper case stream Stream<String> uppercaseStream = intValues.map(string -> string.toUpperCase()); // iterating final stream uppercaseStream.forEach(output -> System.out.println(output)); } }
Output:
JAVA PROGRAM TO COM JAVA-W3SCHOOLS BLOG
6. Java Stream map() example to collect the names into List of String's
User.java
package com.java.w3schools.blog.java.program.to.java8.map; public class User { private int userId; private String name; private String email; public User(int userId, String name, String email) { super(); this.userId = userId; this.name = name; this.email = email; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public String toString() { return "User [userId=" + userId + ", name=" + name + ", email=" + email + "]"; } }
package com.java.w3schools.blog.java.program.to.java8.map; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Java program to collect all user email addresses into List using stream map() * function. * * @author JavaProgramTo.com * */ public class StreamMapExample4 { public static void main(String[] args) { // Creating string stream using Stream.of() method. Stream<User> intValues = Stream.of(new User(100, "jhon", "jhon@gmail.com"), new User(200, "cena", "cena@gmail.com"), new User(300, "burg", "burg@gmail.com")); // list of email addresses List<String> emailList = intValues.map(user -> user.getEmail()).collect(Collectors.toList()); // iterating final stream emailList.forEach(output -> System.out.println(output)); } }
Output:
jhon@gmail.com cena@gmail.com burg@gmail.com
7. Java Stream map() example to collect User objects into List
package com.java.w3schools.blog.java.program.to.java8.map; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; /** * Java program to collect User objects into List using stream map() function. * * @author JavaProgramTo.com * */ public class StreamMapExample6 { public static void main(String[] args) { // Creating string stream using Stream.of() method. Stream<User> intValues = Stream.of(new User(500, "nag", "nag@gmail.com"), new User(700, "remo", "remo@gmail.com"), new User(900, "beri", "beri@gmail.com")); // list of user objects List<User> emailList = intValues.map(user -> { if (user.getEmail().contains("gmail.com")) { return user; } else return user; }).collect(Collectors.toList()); // iterating final User list emailList.forEach(output -> System.out.println(output)); } }
Output:
User [userId=500, name=nag, email=nag@gmail.com] User [userId=700, name=remo, email=remo@gmail.com] User [userId=900, name=beri, email=beri@gmail.com]
8. Conclusion
In this article, We have seen how to use map() method and it's example programs.
No comments:
Post a Comment
Please do not add any spam links in the comments section.