- 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 limit() method in Java
The limit() method of the LongStream class in Java returns a stream consisting of the elements of this stream, truncated to be no longer than max in length. Here, max is the parameter of the method.
The syntax is as follows.
LongStream limit(long max)
Here, max is the number of elements the stream should be limited to.
To use the LongStream class in Java, import the following package.
import java.util.stream.LongStream;
Create a LongStream and add elements.
LongStream longStream = LongStream.of(2000L, 35000L, 45000L, 50500L, 65000L, 72000L);
Now, let’s say you only want to return 4 elements. For that, use the limit as 4.
longStream.limit(4).
The following is an example to implement LongStream limit() method.
Example
import java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream longStream = LongStream.of(2000L, 35000L, 45000L, 50500L, 65000L, 72000L); System.out.println("Remaining elements of the stream..."); longStream.limit(4).forEach(System.out::println); } }
Output
Remaining elements of the stream... 2000 35000 45000 50500
Advertisements