Introduction:
In modern Java programming, streams are a powerful tool for working with collections in a concise and efficient way.
Removing Duplicates from Arrays using Java 8 Streams:
Suppose you have an array of integers with duplicates, and you want to create a new array containing only distinct values then follow below steps
- Convert the array into a stream.
- Use the
distinct()
method to eliminate duplicates. - Convert the stream back into an array using
toArray()
.
import java.util.Arrays;
public class RemoveDuplicatesFromArray {
public static void main(String[] args) {
Integer[] array = {1, 2, 2, 3, 4, 4, 5};
// Step 1: Convert the array into a stream.
// Step 2: Use the `distinct()` method to eliminate duplicates.
// Step 3: Convert the stream back into an array using `toArray()`.
Integer[] distinctArray = Arrays.stream(array)
.distinct()
.toArray(Integer[]::new);
System.out.println(Arrays.toString(distinctArray));
}
}
Removing Duplicates from ArrayList using Java 8 Streams:
Suppose you have an ArrayList of strings with duplicates, and you want to create a new ArrayList with distinct values then follow below steps
- Convert the ArrayList into a stream.
- Use the
distinct()
method to remove duplicates. - Collect the distinct elements into a new ArrayList using
Collectors.toList()
.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicatesFromArrayList {
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
arrayList.add("apple");
arrayList.add("banana");
arrayList.add("apple");
arrayList.add("cherry");
arrayList.add("banana");
// Step 1: Convert the ArrayList into a stream.
// Step 2: Use the `distinct()` method to remove duplicates.
// Step 3: Collect the distinct elements into a new ArrayList using `Collectors.toList()`.
List<String> distinctList = arrayList.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(distinctList);
}
}
Removing Duplicates from List<Employee> using Java 8 Streams:
- We convert the
List
ofEmployee
objects into a stream usingstream()
. - We use the
distinct()
method to remove duplicates based on theequals()
andhashCode()
methods defined in theEmployee
class. - We collect the distinct
Employee
objects into a newList
usingCollectors.toList()
. - The resulting
distinctEmployees
list will contain only uniqueEmployee
objects, and you can iterate through it or perform further operations as needed.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class RemoveDuplicatesFromEmployeeListWithStreams {
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee(1, "Alice"));
employeeList.add(new Employee(2, "Bob"));
employeeList.add(new Employee(1, "Alice")); // Duplicate
employeeList.add(new Employee(3, "Charlie"));
employeeList.add(new Employee(2, "Bob")); // Duplicate
// Step 1: Convert the List of employees into a stream.
// Step 2: Use the distinct() method to remove duplicates based on equals() and hashCode().
// Step 3: Collect the distinct elements into a new List using Collectors.toList().
List<Employee> distinctEmployees = employeeList.stream()
.distinct()
.collect(Collectors.toList());
// Print the distinct Employee objects.
for (Employee employee : distinctEmployees) {
System.out.println(employee.getId() + ": " + employee.getName());
}
}
}
Comments
Post a Comment