![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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 distinct() method in Java
The distinct() method in the LongStream class returns a stream consisting of the distinct elements of this stream.
The syntax is as follows.
LongStream distinct()
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(100L, 150L, 100L, 300L, 150L, 500L);
Now, get the distinct elements.
longStream.distinct().
The following is an example to implement LongStream distinct() method in Java. Here, we have repeated elements in the stream.
Example
import java.util.stream.LongStream; public class Demo { public static void main(String[] args) { LongStream longStream = LongStream.of(100L, 150L, 100L, 300L, 150L, 500L); System.out.println("Distinct elements..."); longStream.distinct().forEach(System.out::println); } }
Here is the output displaying only the distinct elements.
Output
Distinct elements... 100 150 300 500
Advertisements