//CountingStringsOfLength3.java

import java.util.Arrays;
import java.util.List;

public class CountingStringsOfLength3
{
    public static void main(String args[])
    {
        //We will count the strings of length 3 in the following
        //list of strings:
        List<String> strings
            = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl", "mno");
        System.out.println("\nList of strings: " + strings);

        System.out.println("\n=============");
        System.out.println("Using Java 7: ");
        int count = 0;
        for (String string : strings)
        {
            if (string.length() == 3)
            {
                count++;
            }
        }
        System.out.println("Number of strings of length 3: " + count);

        System.out.println("\n=============");
        System.out.println("Using Java 8: ");
        System.out.println
        (
            "Number of strings of length 3: " + strings
            .stream()
            .filter(string -> string.length() == 3)
            .count()
        );
    }
}
/*  Output:

    List of strings: [abc, , bc, efg, abcd, , jkl, mno]

    =============
    Using Java 7:
    Number of strings of length 3: 4

    =============
    Using Java 8:
    Number of strings of length 3: 4
*/

