Learn how to get the second character of a string in Java 8 using charAt(), Stream map(), and flatMap() with simple examples and output.
To get the second character, use charAt(1) because Java string indexing starts from 0.
In this example, map() is more suitable because each string produces exactly one character. flatMap() is generally used when each element produces multiple values.
Code Explanation (Step-by-Step) :
• Arrays.asList() converts the given string values into a list.
• The stream() method converts the list into a stream .
Using map() Method
• The map() method processes each string one by one.
• charAt(1) returns the second character of each string .
• collect() collects the result from the stream.
• Collectors.toList() stores all second characters into a list.
• Now prints the list of second characters obtained using map().
Using flatMap() Method
• The stream() method converts the list into a stream.
• flatMap() is used to transform and flatten stream elements.
• t.charAt(1) gets the second character from each string.
• Stream.of() converts each character into a stream.
• .collect(Collectors.toList()) collects all characters into a list.
• Now prints the list of second characters obtained using flatMap().
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SecondChar {
public static void main(String[] args) {
List list = Arrays.asList("grapes", "mango", "pineapple", "coconut");
List check = list.stream()
.map(s -> s.charAt(1))
.collect(Collectors.toList());
System.out.println("Second character using map: " + check);
List ch = list.stream()
.flatMap(t -> Stream.of(t.charAt(1)))
.collect(Collectors.toList());
System.out.println("Second character using flatMap: " + ch);
}
}
Output :
Second character using map: [r, a, i, o]
Second character using flatMap: [r, a, i, o]
Difference Between map() and flatMap() in Java 8
In Java 8 Stream API, both map() and flatMap() are intermediate operations, but they are used for different way.
What is map() in Java 8?
The map() method is used to transform each element of a stream into another single element.
When to use map()?
Use map() when one input element produces one output element.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Test2 {
public static void main(String[] args) {
List list = Arrays.asList("grapes", "mango", "pineapple", "coconut");
List result = list.stream()
.map(s -> s.charAt(3))
.collect(Collectors.toList());
System.out.println(result);
}
}
Output :
[p, g, e, o]
What is flatMap() in Java 8?
The flatMap() method is used when each element is transformed into a stream of multiple elements, and then all streams are combined into one single stream.
When to use flatMap()?
Use flatMap() when one input element produces multiple output elements.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Test2 {
public static void main(String[] args) {
List> names = Arrays.asList(
Arrays.asList("Ajay", "Pankaj"),
Arrays.asList("Vijay", "Sanjay"));
List result = names.stream()
.flatMap(list -> list.stream())
.collect(Collectors.toList());
System.out.println(result);
}
}
Output :
[Ajay, Pankaj, Vijay, Sanjay]
FAQ
Why do we use charAt(1) for the second character?
In Java, string indexing starts from 0. Therefore, the first character is at index 0, and the second character is at index 1.
What happens if the string length is less than 2?
If the string length is less than 2, charAt(1) will throw a StringIndexOutOfBoundsException. To avoid this, we should check the string length before accessing the second character.
Can we get the second character from a list of strings using Java 8 Stream?
Yes, we can use Java 8 Stream with the map() method to get the second character from each string in a list.
list.stream().map(s -> s.charAt(1))
.collect(Collectors.toList());
How do you get the second character of a string in Java?
You can get the first character of a string in Java by using the charAt(1) method.
String str = "Apple";
char ch = str.charAt(1);
System.out.println(ch);
Output :
A