Java IntStream distinct() Method

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The distinct() method in Java, part of the java.util.stream.IntStream interface, is used to return a stream consisting of the distinct elements of the original stream. This method is useful when you need to remove duplicates from a stream of integers.

Table of Contents

  1. Introduction
  2. distinct() Method Syntax
  3. Understanding distinct()
  4. Examples
    • Basic Usage
    • Using distinct() with Other Stream Operations
  5. Real-World Use Case
  6. Conclusion

Introduction

The distinct() method returns a stream consisting of the distinct elements of the original stream, effectively removing any duplicates. This method is an intermediate operation, meaning it returns a new stream and does not modify the original stream.

distinct() Method Syntax

The syntax for the distinct() method is as follows:

IntStream distinct() 

Parameters:

  • This method does not take any parameters.

Returns:

  • A new IntStream consisting of the distinct elements of the original stream.

Throws:

  • This method does not throw any exceptions.

Understanding distinct()

The distinct() method processes the elements of the stream and removes any duplicates, ensuring that the resulting stream contains only unique elements. This is useful for scenarios where you need to work with unique values or when duplicates need to be eliminated for further processing.

Examples

Basic Usage

To demonstrate the basic usage of distinct(), we will create an IntStream with duplicate elements and use distinct() to remove the duplicates.

Example

import java.util.stream.IntStream; public class DistinctExample { public static void main(String[] args) { IntStream intStream = IntStream.of(1, 2, 2, 3, 3, 3, 4, 5, 5); // Use distinct() to remove duplicates IntStream distinctStream = intStream.distinct(); // Print the distinct elements distinctStream.forEach(System.out::println); } } 

Output:

1 2 3 4 5 

Using distinct() with Other Stream Operations

This example shows how to use distinct() in combination with other stream operations, such as filtering and mapping.

Example

import java.util.stream.IntStream; public class DistinctWithOtherOperationsExample { public static void main(String[] args) { IntStream intStream = IntStream.of(1, 2, 2, 3, 3, 3, 4, 5, 5); // Use distinct(), filter, and map the elements IntStream processedStream = intStream.distinct() .filter(n -> n % 2 == 0) .map(n -> n * 10); // Print the processed elements processedStream.forEach(System.out::println); } } 

Output:

20 40 

Real-World Use Case

Removing Duplicate Scores

In real-world applications, the distinct() method can be used to remove duplicate scores from a stream of student scores before further processing.

Example

import java.util.stream.IntStream; public class RemoveDuplicateScoresExample { public static void main(String[] args) { IntStream scores = IntStream.of(85, 92, 85, 90, 92, 75, 90); // Use distinct() to remove duplicate scores IntStream uniqueScores = scores.distinct(); // Print the unique scores uniqueScores.forEach(score -> System.out.println("Unique score: " + score)); } } 

Output:

Unique score: 85 Unique score: 92 Unique score: 90 Unique score: 75 

Conclusion

The IntStream.distinct() method is used to remove duplicates from a stream, returning a stream consisting of the distinct elements of the original stream. This method is particularly useful for scenarios where you need to work with unique values. By understanding and using this method, you can efficiently manage and process streams of integer values in your Java applications.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare