In Java 8 streams map() method is one of the most important and the widely used methods of streams. In this tutorial, we would be looking at various ways we can use map method. These examples can help you understand the usage of Java 8 stream map() method. The javadocs describes the example:() method as:
Returns a stream consisting of the results of applying the given function to the elements of this stream.
Examples of map method
1. Convert a list of Strings to a list of Integers
The map() method provides a way to convert an input object to a different/same output object. Here we will transform a simple list of numbers as strings to a list of Integers.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class ConvertListOfNumbersAsStringToIntegerList { public static void main(String[] args) { List<String> numbersAsString = Arrays.asList("1", "2", "1", "4", "1", "8", "6"); List<Integer> integerList = convertNumbersAsString2Integers(numbersAsString); integerList.forEach(System.out::println); } private static List<Integer> convertNumbersAsString2Integers(List<String> numbersAsString) { return numbersAsString.stream().map(Integer::parseInt).collect(Collectors.toList()); } } |
2. Change the case of Strings
We looked at converting the type of object above. Now we will do an operation on the items in a list and create a new list. A simple example that changes the case of the Strings of the list.
1 2 3 4 5 6 7 8 9 10 11 12 |
public class ChangeCaseStrings { public static void main(String[] args) { List<String> knownPerson = Arrays.asList("Joe", "Trump", "Obama", "Bush", "Clinton", "Bernie", "Kelly"); List<String> lowerCaseKnownPersons = changeCase(knownPerson); lowerCaseKnownPersons.forEach(System.out::println); } private static List<String> changeCase(List<String> numbersAsString) { return numbersAsString.stream().map(String::toLowerCase).collect(Collectors.toList()); } } |
3. Mixing Map with filter
You can use map() method with other useful functions provided by Java 8 stream framework. In the below example, we would filter all the students that have percentage higher than 75%. And then get the names of these students and print them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class MixMapWithFilterJava8 { public static void main(String[] args) { List<Student> students = Arrays.asList(new Student("Andre", 76), new Student("Christian", 81), new Student("Haden", 65)); List<String> studentsNamesWithHigherGrades = students.stream().filter(student -> student.getPercentage() >= 75) .map(student -> student.getName()) .collect(Collectors.toList()); studentsNamesWithHigherGrades.forEach(each -> System.out.println(each)); } public static class Student { private final String name; private final int percentage; public Student(String name, int percentage) { this.name = name; this.percentage = percentage; } public int getPercentage() { return percentage; } public String getName() { return name; } } } |
4. An interesting example : Map And create a phrase with it
Let’s make the above example a little bit more interesting and create a sentence out of the names we received in the above example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class MapAndCreatePhrase { public static void main(String[] args) { List<Student> students = Arrays.asList(new Student("Andre", 76), new Student("Christian", 81), new Student("Haden", 65)); String grades = students.stream().filter(student -> student.getPercentage() >= 75) .map(student -> student.getName()) .collect(Collectors.joining(" and ", "In Class IX ", " have recieved highest grades.")); System.out.println(grades); // In Class IX Andre and Christian have recieved highest grades. } } |
Summary
We looked at some interesting ways we can use the map method. I hope that usage of map() method is more clear to you after reading and practicing the above examples. In case you have some more interesting examples, please share in comments.