To remove first character from list of string in java 8, we are using the Stream Api and subString() method.
- First, we take a Arraylist object with String type parameter.
- Now get the Stream data from List using list.stream() method.
- Inside map function, a.substring(1, a.length()) is called which return the string starting from the character at index 1 and ending index will be a.length().
- From collect() method, we collect all the list of string data.
- Print the list of string from second character.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CharacterTest {
public static void main(String[] args) {
List<String> list = Arrays.asList("joe", "mary");
List<String> names = list.stream().map(a -> a.substring(1, a.length())).collect(Collectors.toList());
System.out.println("list of string are:- " + names);
}
}
Output :-
list of string are:- [oe, ary]