0% found this document useful (0 votes)
5 views1 page

Remove Duplicates from String List

The document describes a Java program that removes duplicate strings from a list using the Stream API without lambda expressions. It creates a list of strings, converts it to a stream, applies the distinct() method to filter out duplicates, and collects the results into a new list. The program then prints both the original list and the list with duplicates removed.

Uploaded by

Subhajit Kundu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Remove Duplicates from String List

The document describes a Java program that removes duplicate strings from a list using the Stream API without lambda expressions. It creates a list of strings, converts it to a stream, applies the distinct() method to filter out duplicates, and collects the results into a new list. The program then prints both the original list and the list with duplicates removed.

Uploaded by

Subhajit Kundu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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

lambda expressions:

import [Link];
import [Link];
import [Link];
import [Link];

public class RemoveDuplicates {


public static void main(String[] args) {
List<String> strings = [Link]("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 = [Link]()
.distinct()
.collect([Link]());

// Print the original list and the list with duplicates removed
[Link]("Original List: " + strings);
[Link]("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([Link]()).
Finally, we print both the original list and the list with duplicates removed.

You might also like