Java program to check if a list contains a specific word using lambda expression
Java Lambda Program: Exercise-16 with Solution
Write a Java program to implement a lambda expression to check if a list of strings contains a specific word.
Sample Solution:
Java Code:
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
// Create a list of strings
List < String > colors = Arrays.asList("Red", "Green", "Blue", "Orange", "Black");
// Specify the word to search for
String searchColor = "Orange";
// Check if the list contains the specified color using a lambda expression
Predicate < String > containsWord = word -> word.equals(searchColor);
boolean flag = colors.stream().anyMatch(containsWord);
// Print the result
System.out.println("Is the word " + searchColor + " present in the list? " + flag);
// Specify the word to search for
String searchColor1 = "White";
// Check if the list contains the specified color using a lambda expression
Predicate < String > containsWord1 = word -> word.equals(searchColor1);
flag = colors.stream().anyMatch(containsWord1);
// Print the result
System.out.println("\nIs the word " + searchColor1 + " present in the list? " + flag);
}
}
Sample Output:
Is the word Orange present in the list? true Is the word White present in the list? false
Explanation:
In the above exercise -
- Import the necessary classes: Arrays, List, and Predicate.
- In the main method, we create a list of strings called colors using Arrays.asList().
- Specify the word we want to search for by assigning it to the variable searchColor.
- Define a lambda expression to check if a string equals the searchColor using the Predicate functional interface. The lambda expression compares each string with the searchColor using the equals() method.
- Use the stream() method on the colors list to create a stream of strings.
- Use the anyMatch() method along with the containsWord predicate to check if any element in the stream matches the predicate.
- The result of the anyMatch() operation is stored in the boolean variable flag.
Finally, we print the result, indicating whether the list contains the specified color.
Flowchart:

Live Demo:
Java Code Editor:
Improve this sample solution and post your code through Disqus
Java Lambda Exercises Previous: Java program to calculate sum of squares of odd and even numbers using lambda expression.
Java Lambda Exercises Next: Java program to find length of longest and smallest string using lambda expression.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
How do I remove repeated elements from ArrayList?
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);
Of course, this destroys the ordering of the elements in the ArrayList.
Ref: https://bit.ly/3bYIjNC
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook