print all the words in a string that start and end with same character using java 8 ?

To print all the words in a string that start and end with same character using java 8,here first we create a list.
now we stream the list of data.after stream we use filter method.in filter we check string length should be greater than zero.from charAt() method we get the character at given index and pass this value inside endWith() method.

				
					import java.util.Arrays;
import java.util.List;

public class StartAndEndSameChar {
	public static void main(String[] args) {
		List<String> l = Arrays.asList("abc", "mnm", "xyx", "wer", "aba");
		l.stream().filter(e -> e.length() > 0 && e.endsWith(String.valueOf(e.charAt(0)))).
		forEach(System.out::println);
	}
}
				
			

Output :-
mnm
xyx
aba

Leave a Comment

Your email address will not be published. Required fields are marked *