How to count the number of characters in array of strings with Java

1 Answer

0 votes
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<String> arr = Arrays.asList("c#", "c", "java", "python", "c++");

        int totalCharacters = arr.stream().mapToInt(String::length).sum();

        System.out.println(totalCharacters);
    }
}

   
/*
run:
   
16
   
*/

 



answered Aug 20, 2024 by avibootz
edited Aug 20, 2024 by avibootz
...