Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

a list of strings, removes duplicate strings using the Stream API, and avoids using

lambda expressions:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class RemoveDuplicates {


public static void main(String[] args) {
List<String> strings = Arrays.asList("apple", "banana", "apple", "orange",
"banana", "grape");

// Convert the list to a stream, use distinct() to remove duplicates, and


collect the result into a new list
List<String> distinctStrings = strings.stream()
.distinct()
.collect(Collectors.toList());

// Print the original list and the list with duplicates removed
System.out.println("Original List: " + strings);
System.out.println("List with Duplicates Removed: " + distinctStrings);
}
}

==============================================

We create a list of strings strings.


We convert the list to a stream using stream().
We use the distinct() method to remove duplicate elements from the stream.
We collect the elements of the stream into a new list using
collect(Collectors.toList()).
Finally, we print both the original list and the list with duplicates removed.

You might also like