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

Lab11 Java

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)
16 views1 page

Lab11 Java

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
You are on page 1/ 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