Write a Java program using Lambda Expression to check if all strings in a list start with an uppercase letter


The java program that checks if all the strings in a list start with an uppercase letter using Java Streams. Here's an explanation of the code:

  • An ArrayList named fruits is created to store a list of strings. Some of the strings in the list start with an uppercase letter, while others do not.
  • The program uses Java Streams to check if all the strings start with an uppercase letter:
    • fruits.stream(): This converts the fruits list into a stream of strings.
    • .allMatch(s -> Character.isUpperCase(s.charAt(0))): This is a stream operation that checks if all elements in the stream satisfy the specified condition. In this case, the condition is that the first character of each string (s.charAt(0)) is an uppercase letter (Character.isUpperCase(...)).
  • The result of the .allMatch(...) operation is a boolean value, which indicates whether all the strings in the list meet the specified condition.
  • System.out.println("All start with Uppercase : " + uppercase_start); prints whether all the strings in the list start with an uppercase letter.

Source Code

import java.util.ArrayList;
import java.util.List;
 
public class AllUpperCaseStart
{
	public static void main(String[] args)
	{		
		List<String> fruits = new ArrayList<>();
		fruits.add("Apple");
		fruits.add("Banana");
		fruits.add("cherry");
		fruits.add("Oranges");
		fruits.add("Date");
 
		boolean uppercase_start = fruits.stream().allMatch(s -> Character.isUpperCase(s.charAt(0)));
 
		System.out.println("All start with Uppercase : " + uppercase_start);
	}
}

Output

All start with Uppercase : false

Example Programs