I have a String array in java as follows.
String[] text = {"hello", "bye"};
Now I need to use Java stream API to calculate the length of each string in this array. I do not want to use for loop. How can I do that?
Update: I have checked this link and got the following code to get the sum of the length of each string of this array.
Arrays.stream(text)
.filter(Objects::nonNull)
.mapToInt(String::length)
.reduce(0,Integer::sum);
CodePudding user response:
You could simply collect after mapping to String::length
:
List<Integer> length = Arrays.stream(text)
.filter(Objects::nonNull)
.map(String::length)
.toList();
This would give you a list with the lengths in the same order as defined in the array. (If below JDK 16, you need to use collect(Collectors.toList())
instead of toList()
).
Another option would be to collect to a Map
:
Map<String, Integer> stringToLength = Arrays.stream(text)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toMap(Function.identity(), String::length));
This would give you a map with the strings as keys, and the length as values. distinct()
is used in order to protect yourself from multiple copies of the same string, the map collecting would throw an exception in case of duplicates.
CodePudding user response:
Since you are starting with an array you may want to return an array so this will work.
String[] text = {"hello", "bye"};
int[] lengths = Arrays.stream(text)
.mapToInt(String::length)
.toArray();
System.out.println(Arrays.toString(lengths));
prints
[5, 3]
CodePudding user response:
tl;dr
Use code points rather than char
& String#length
.
Arrays
.stream( inputs ) // A stream of each `String` object in the array.
.mapToLong( s -> s.codePoints().count() ) // Get the count of code points for each string. Returns a `LongStream`.
.toArray() ; // Returns an array of `long` primitives.
Code points
Your code snippet and the other Answers use String#length
. Unfortunately that method is legacy, based on the legacy type char
.
That approach fails with most characters. Here is a demonstration of such a failure.
String input = "A