Java Stream mapToInt() with examples

431 total views, 2 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream mapToInt() with examples.

mapToInt() is a part of the Stream pipeline and an intermediate operation. It belongs to the Stream interface.

mapToInt() operation will transform each element of the stream to Integer form according to the provided function.It returns a IntStream.

Method description

IntStream mapToInt(ToIntFunction<? super T> mapper)

As per official documentation

Returns an IntStream consisting of the results of applying the given function to the elements of this stream.

mapToInt() accepts a ToIntFunction interface.A ToIntFunction interface takes input as an argument and returns a value of Integer type.

Since mapToInt() is an intermediate operation it will have two characteristics.

  1. mapToInt() is lazy loading operation. This means data will only be mapped when a terminal operation is invoked on the stream.
  2. mapToInt() operation on a stream will always create a new stream output.

Let us now see a few examples of Java 8 stream mapToInt()

Java stream mapToInt() example

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");
		IntStream listOfIntegers = list.stream()
                              .mapToInt(item -> Integer.valueOf(item));
	}
}

Java stream mapToInt() example to convert Stream of String to Integer

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");
		list.stream()
		    .mapToInt(item -> Integer.valueOf(item))
		    .forEach(System.out::println);
	}
}

Output

1
2
3
4
5

Java stream mapToInt() with ToIntFunction interface example

import java.util.ArrayList;
import java.util.List;
import java.util.function.ToIntFunction;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");

		ToIntFunction<String> function = (String item) ->  
                                                 Integer.valueOf(item); 
          list.stream().mapToInt(function).forEach(System.out::println);
	}
}

Conclusion

Java Stream mapToInt() operation is mainly used for transforming one form of the stream to Integer form. mapToInt() will always create a new stream and it is lazily evaluated.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.