Introduction
Duplicate elements in an array can lead to unnecessary data processing and incorrect results in many applications. Java 8 provides an efficient way to remove duplicates from an array using the Stream API. In this guide, we will learn how to remove duplicates from arrays in Java 8.
Problem Statement
Write a Java program that:
- Takes an array as input.
- Removes duplicate elements.
- Displays the array without duplicates.
Example:
- Input:
int[] array = {1, 2, 2, 3, 4, 4, 5}
- Output:
[1, 2, 3, 4, 5]
Solution Steps
- Define the Input Array: Provide an array containing some duplicate elements.
- Use Java 8 Streams: Convert the array into a stream, apply
distinct()
to remove duplicates. - Collect the Result: Convert the stream back into an array.
- Display the Result: Print the array without duplicates.
Java Program to Remove Duplicates from an Integer Array
import java.util.Arrays;
public class RemoveDuplicates {
public static void main(String[] args) {
// Step 1: Define the input array
int[] array = {1, 2, 2, 3, 4, 4, 5};
// Step 2: Use Streams to remove duplicates
int[] uniqueArray = Arrays.stream(array)
.distinct() // Remove duplicates
.toArray(); // Convert back to an array
// Step 3: Display the result
System.out.println("Array without duplicates: " + Arrays.toString(uniqueArray));
}
}
Output
Array without duplicates: [1, 2, 3, 4, 5]
Explanation
Arrays.stream(array)
: Converts the array into anIntStream
.distinct()
: Filters the stream to remove duplicate elements.toArray()
: Collects the filtered stream back into an array.Arrays.toString()
: Prints the resulting array.
Java Program to Remove Duplicates from a String Array
The same method can be applied to a string array. Let’s filter out duplicate strings from a string array.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveStringDuplicates {
public static void main(String[] args) {
// Step 1: Define the input array
String[] stringArray = {"Apple", "Banana", "Apple", "Orange", "Banana"};
// Step 2: Use Streams to remove duplicates
List<String> uniqueStrings = Arrays.stream(stringArray)
.distinct() // Remove duplicates
.collect(Collectors.toList()); // Convert back to a list
// Step 3: Display the result
System.out.println("Array without duplicates: " + uniqueStrings);
}
}
Output
Array without duplicates: [Apple, Banana, Orange]
Explanation
Arrays.stream(stringArray)
: Converts the string array into aStream<String>
.distinct()
: Removes duplicates from the stream.collect(Collectors.toList())
: Collects the result into a list instead of an array, which is a common approach for strings.
Java Program to Remove Duplicates from an Array of Custom Objects (Using Employee
Class)
In this example, we’ll use an Employee
class and remove duplicate employees based on their name.
Example
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveEmployeeDuplicates {
public static void main(String[] args) {
// Step 1: Define the input array of Employee objects
Employee[] employees = {
new Employee("John", 55000),
new Employee("Alice", 48000),
new Employee("John", 60000), // Duplicate based on name
new Employee("Bob", 45000)
};
// Step 2: Remove duplicates based on employee name
List<Employee> uniqueEmployees = Arrays.stream(employees)
.distinct() // This would only work if Employee class implements equals and hashCode properly
.collect(Collectors.toList());
// Step 3: Display the result
System.out.println("Employees without duplicates: " + uniqueEmployees);
}
}
// Employee class with equals() and hashCode() implemented
class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return name.equals(employee.name);
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public String toString() {
return name + " (Salary: " + salary + ")";
}
}
Output
Employees without duplicates: [John (Salary: 55000), Alice (Salary: 48000), Bob (Salary: 45000)]
Explanation
equals()
andhashCode()
: We implement these methods in theEmployee
class to ensure that employees are considered equal based on theirname
.distinct()
: This works on custom objects only when theequals()
andhashCode()
methods are implemented properly.collect(Collectors.toList())
: Collects the filtered employees into a list.
Conclusion
In Java 8, removing duplicates from an array is simple and efficient with the Stream API. By using the distinct()
method, you can easily remove duplicates from arrays of integers, strings, or even custom objects. For custom objects, ensure that the equals()
and hashCode()
methods are implemented to correctly identify duplicates.
Comments
Post a Comment
Leave Comment