🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Removing duplicate elements from a list is a common task in data processing. With Java 8, this can be efficiently done using streams. Streams provide a declarative way to process collections, including operations like filtering, mapping, and reducing.
Problem Statement
Create a Java program that:
- Takes a list of elements (with possible duplicates).
- Removes the duplicate elements using Java 8 Streams.
- Returns and displays the list with duplicates removed.
Example 1:
- Input:
[1, 2, 2, 3, 4, 4, 5] - Output:
[1, 2, 3, 4, 5]
Example 2:
- Input:
["apple", "orange", "apple", "banana"] - Output:
["apple", "orange", "banana"]
Solution Steps
- Create the List: Initialize a list with duplicate elements.
- Use Java 8 Stream to Remove Duplicates: Convert the list to a stream, use the
distinct()method to remove duplicates, and collect the result back into a list. - Display the Result: Print the list with duplicates removed.
Java Program
Java 8 Program to Remove Duplicate Elements from a List
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Java 8 Program to Remove Duplicate Elements from a List
* Author: https://www.javaguides.net/
*/
public class RemoveDuplicates {
public static void main(String[] args) {
// Example 1: Integer List
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
List<Integer> uniqueNumbers = numbers.stream()
.distinct() // Remove duplicates
.collect(Collectors.toList());
System.out.println("Unique numbers: " + uniqueNumbers);
// Example 2: String List
List<String> fruits = Arrays.asList("apple", "orange", "apple", "banana");
List<String> uniqueFruits = fruits.stream()
.distinct() // Remove duplicates
.collect(Collectors.toList());
System.out.println("Unique fruits: " + uniqueFruits);
}
}
Explanation
Input: The program initializes lists containing duplicate elements.
Removing Duplicates Using Stream:
- The
stream()method is called on the list to convert it into a stream. - The
distinct()method is used to remove duplicate elements. This method returns a stream consisting of the distinct elements of the list. - The
collect(Collectors.toList())method collects the elements of the stream back into a list.
- The
Output: The program prints the list with duplicates removed.
Output Example
Example 1 (Integer List):
Unique numbers: [1, 2, 3, 4, 5]
Example 2 (String List):
Unique fruits: ["apple", "orange", "banana"]
Example 3 (List of Custom Objects):
In case you want to remove duplicates from a list of custom objects (like Person), you would need to override the equals() and hashCode() methods in your Person class to ensure that the distinct() method works correctly.
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
@Override
public String toString() {
return name + " (" + age + ")";
}
}
public class RemoveDuplicatesCustomObjects {
public static void main(String[] args) {
List<Person> people = Arrays.asList(
new Person("John", 25),
new Person("Alice", 30),
new Person("John", 25),
new Person("Bob", 20)
);
List<Person> uniquePeople = people.stream()
.distinct() // Remove duplicates based on equals and hashCode
.collect(Collectors.toList());
System.out.println("Unique people: " + uniquePeople);
}
}
Output Example
Example 3 (List of Custom Objects):
Unique people: [John (25), Alice (30), Bob (20)]
Explanation
- Custom Object List: The
Personclass has overridden theequals()andhashCode()methods to ensure that twoPersonobjects are considered equal if theirnameandagefields are the same. Thedistinct()method then removes any duplicatePersonobjects based on this equality check.
Conclusion
This Java 8 program demonstrates how to remove duplicate elements from a list using streams and the distinct() method. The program covers simple lists of integers and strings, as well as lists of custom objects. Using Java 8 streams makes this operation concise and efficient, and the program can be easily extended to handle more complex scenarios.
Comments
Post a Comment
Leave Comment