- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
LongStream mapToInt() method in Java
The mapToInt() method returns an IntStream consisting of the results of applying the given function to the elements of this stream.
The syntax is as follows
mapToInt(LongToIntFunction mapper)
Here, the parameter mapper is the stateless function applied to each element.
Declare LongStream and add some elements
LongStream longStream = LongStream.of(1000L, 13000L, 18000L);
Now, use the IntStream and mapToInt()
IntStream intStream = longStream.mapToInt(val -> (int) val);
The following is an example to implement LongStream mapToInt() in Java
Example
import java.util.*; import java.util.stream.IntStream; import java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream longStream = LongStream.of(1000L, 13000L, 18000L); IntStream intStream = longStream.mapToInt(val -> (int) val); intStream.forEach(System.out::println); } }
Output
1000 13000 18000
Advertisements