Here are Print all the words in a list of string which start and end with same letter in java 8.
- First convert Arrays to list using Arrays.asList() method.
- Now get the Stream data from List using arrayList.stream() method.
- Inside filter, first we check length of string should be greater than 0, using r.length()>0 method.
- From r.endWith() we find last character and start character we get from r.charAt(0), after we compare both the character.
- Now collect() method, we collect all the list of string.
- Print the starts and ends with same letter.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SameWord {
public static void main(String[] args) {
List<String> list = Arrays.asList("abc", "aba", "xyz", "sta", "pap");
List<String> data = list.stream().filter(r -> r.length() > 0 && r.endsWith(String.valueOf(r.charAt(0)))).collect(Collectors.toList());
System.out.println("same word are :- " + data);
}
}
Output :-
same word are :- [aba, pap]