Java Stream distinct() Method with Examples
The distinct()
method in Java is a part of the java.util.stream.Stream
interface, and it is used to return a stream consisting of the distinct elements of the original stream. In this guide, we will learn how to use distinct()
method in Java with practical examples and real-world use cases to better understand its functionality.
Learn Java from scratch: Learn Java Programming.
Complete Java Reference: Java API Documentation.
Table of Contents
- Introduction
distinct()
Method Syntax- Examples
- Real-World Use Case
- Conclusion
Introduction
The Stream.distinct()
method in Java is used to remove duplicate elements from a stream. It ensures that only unique elements are processed.
This method is useful when you want to filter out duplicate values and work with distinct data in your stream.
distinct()
is often used with other stream operations like filter()
or map()
to refine the stream’s content before further processing.
distinct() Method Syntax
The syntax for the distinct()
method is as follows:
Stream<T> distinct()
Parameters:
- This method does not take any parameters.
Returns:
- A new
Stream
consisting of the distinct elements of the original stream.
Throws:
- This method does not throw any exceptions.
Examples
Basic Usage
To demonstrate the basic usage of distinct()
, we will create a Stream
with duplicate elements and use distinct()
to remove the duplicates.
import java.util.stream.Stream;
public class DistinctExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "apple", "cherry", "banana");
// Use distinct() to remove duplicates
Stream<String> distinctStream = stream.distinct();
// Print the distinct elements
distinctStream.forEach(System.out::println);
}
}
Output:
apple
banana
cherry
Using distinct()
with Other Stream Operations
This example shows how to use distinct()
in combination with other stream operations, such as filtering and mapping.
import java.util.stream.Stream;
public class DistinctWithOtherOperationsExample {
public static void main(String[] args) {
Stream<String> stream = Stream.of("apple", "banana", "apple", "cherry", "banana");
// Filter elements starting with 'a', remove duplicates, and convert to uppercase
Stream<String> processedStream = stream.filter(s -> s.startsWith("a"))
.distinct()
.map(String::toUpperCase);
// Print the processed elements
processedStream.forEach(System.out::println);
}
}
Output:
APPLE
Real-World Use Case: Removing Duplicate Names
In real-world applications, the distinct()
method can be used to remove duplicate names from a stream of names.
import java.util.stream.Stream;
public class RemoveDuplicateNamesExample {
public static void main(String[] args) {
Stream<String> names = Stream.of("Alice", "Bob", "Alice", "Charlie", "Bob");
// Use distinct() to remove duplicate names
Stream<String> distinctNames = names.distinct();
// Print the distinct names
distinctNames.forEach(name -> System.out.println("Name: " + name));
}
}
Output:
Name: Alice
Name: Bob
Name: Charlie
Conclusion
The Stream.distinct()
method is used to return a stream consisting of the distinct elements of the original stream. This method is particularly useful for scenarios where you need to remove duplicates and work with unique values.
By understanding and using this method, you can efficiently manage and process streams of values in your Java applications, ensuring that only distinct elements are considered for further operations.
Original Post:
Learn Java from scratch: Learn Java Programming.
Complete Java Reference: Java API Documentation.